/** * @Route("/card/generate", name="generate_cards") */ public function generateCardsAction(Request $request) { if (!$this->getRequest()->isXmlHttpRequest()) { throw $this->createNotFoundException(); } $em = $this->getDoctrine()->getManager(); /**/ $entity = new Card(); $form = $this->createForm(new CardGenerate(), $entity); $form->handleRequest($request); $expiryPeriod = $form->get("expiryPeriod")->getData(); $series = $form->get("series")->getData(); $amount = $form->get("amount")->getData(); if ($amount && $series && $expiryPeriod) { list($currentNumber, $series) = $this->getMaxNumberOfSeries($series); for ($i = 0; $i < $amount; $i++) { $newCard = new Card(); $newCard->setSeries($series); $newCard->setNumber(str_pad(++$currentNumber, 8, "0", STR_PAD_LEFT)); $newCard->setIssueDate(new \DateTime()); $expiryDate = new \DateTime(); $newCard->setExpiryDate($expiryDate->modify('+' . $expiryPeriod . ' months')); $newCard->setAmount(0); $newCard->setStatus(Card::STATUS_INACTIVE); $em->persist($newCard); } $em->flush(); $cards = $em->getRepository('AppBundle:Card')->findAll(); $view = $this->renderView('card/partial/card_list.html.twig', array('cards' => $cards)); $response = new Response($view); return $response; } else { return; } }