Esempio n. 1
0
 /**
  * @Route("/admin/stock/{id}/edit")
  */
 public function updateStock(Request $request, $id)
 {
     $entityManager = $this->getDoctrine()->getManager();
     $soda = $this->getDoctrine()->getRepository('AppBundle:Soda')->find($id);
     if (!$soda) {
         throw $this->createNotFoundException('No soda found');
     }
     $stock = $this->getDoctrine()->getRepository('AppBundle:Stock')->find($soda->getStock()->getId());
     if ($stock === null) {
         $stock = new Stock();
     }
     $isSodaInStock = $stock->getSoda($soda->getid());
     if (!$isSodaInStock) {
         $stock->addSoda($soda);
         $entityManager->persist($stock);
         $entityManager->flush();
     }
     $form = $this->createForm(new StockType(), $stock, array('method' => 'POST'));
     $form->add('save', 'submit', array('label' => 'Update Stock for ' . $soda->getName()));
     $form->handleRequest($request);
     if ($form->isValid()) {
         $entityManager->persist($stock);
         $entityManager->flush();
         return $this->redirectToRoute('admin_overview');
     }
     return $this->render('admin/Stock.html.twig', array('form' => $form->createView()));
 }
Esempio n. 2
0
 /**
  * Gets quotes by their symbols
  *
  * @param $symbols array|string
  * @return Stock[]
  */
 public function getStock($symbols)
 {
     if (is_string($symbols)) {
         $symbols = [$symbols];
     }
     /** @var Stock[] $storedStocks */
     $storedStocks = $this->em->getRepository('AppBundle:Stock')->findBySymbol($symbols);
     $resultStocks = $stocksToUpdate = $quotes = [];
     foreach ($symbols as $symbol) {
         if (isset($storedStocks[$symbol])) {
             $stock = $storedStocks[$symbol];
             $totalTime = $stock->getLastUpdate()->modify($this->ttl);
             if ($totalTime < new \DateTime()) {
                 $stocksToUpdate[] = $symbol;
             } else {
                 $resultStocks[] = $stock;
             }
         } else {
             $stocksToUpdate[] = $symbol;
         }
     }
     if ($stocksToUpdate) {
         $quotes = $this->api->getQuotes($stocksToUpdate);
     }
     if ($quotes) {
         foreach ($quotes as $quote) {
             $quoteSymbol = strtoupper($quote['Symbol']);
             if (isset($storedStocks[$quoteSymbol])) {
                 $stock = $storedStocks[$quoteSymbol];
                 $stock->setChangeInPercent($quote['ChangeinPercent'])->setLastTradePrice($quote['LastTradePriceOnly'])->setLastUpdate(new \DateTime());
                 $resultStocks[] = $stock;
             } else {
                 $stock = new Stock();
                 $stock->setSymbol($quoteSymbol)->setCompanyName($quote['Name'])->setChangeInPercent($quote['ChangeinPercent'])->setLastTradePrice($quote['LastTradePriceOnly'])->setStockExchange($quote['StockExchange']);
                 $this->em->persist($stock);
                 $resultStocks[] = $stock;
             }
         }
         $this->em->flush();
     }
     return $resultStocks;
 }
Esempio n. 3
0
 /**
  * @Route("/admin/soda/add")
  */
 public function createNewSoda(Request $request)
 {
     $entityManager = $this->getDoctrine()->getManager();
     $soda = new Soda();
     $form = $this->createForm(new SodaType(), $soda);
     $form->add('save', 'submit', array('label' => 'Create Soda'));
     $form->handleRequest($request);
     if ($form->isValid()) {
         $entityManager->persist($soda);
         $entityManager->flush();
         $stock = new Stock();
         $stock->setId($soda->getId());
         $entityManager->persist($stock);
         $stock->addSoda($soda);
         $stock->setInStock(0);
         $entityManager->flush();
         return $this->redirectToRoute('admin_overview');
     }
     return $this->render('admin/Soda.html.twig', array('form' => $form->createView()));
 }
 /**
  * delete stock entity from the product detail table
  * @Route("/delete_stock_2/{id}",name="deletestock2")
  * @ParamConverter("stock", class="AppBundle:Stock")
  */
 public function delete_stock_2(Stock $stock)
 {
     $user_id = $stock->getUser()->getId();
     $em = $this->getDoctrine()->getManager();
     $em->remove($stock);
     $em->flush();
     echo "<script>alert('删除成功!')</script>";
     return $this->redirectToRoute('productdetail', array('id' => $user_id));
 }
 /**
  * delete stock entity from the product detail table
  * @Route("/delete_stock/{id}",name="deletestock2")
  * @ParamConverter("stock", class="AppBundle:Stock")
  */
 public function delete_stock(Stock $stock)
 {
     $client = $stock->getClient();
     $client_id = $client->getId();
     $em = $this->getDoctrine()->getManager();
     $em->remove($stock);
     $em->flush();
     if (count($client->getStocks()) == 0) {
         $client->setIfStockPurchased(false);
         $em->flush();
     }
     return $this->redirectToRoute('productdetail', array('id' => $client_id));
 }
Esempio n. 6
0
 public function calculateSeries(Stock $stock)
 {
     $this->loader->setCode($stock->getCode());
     $this->loader->setCount($stock->getCount());
     return array_combine($this->loader->getDates(), $this->loader->getPrices());
 }