/** * Accept or reject a deal * * @param Request $request * @return RedirectResponse */ public function acceptRejectAction(Request $request) { /** @var ObjectManager $em */ $em = $this->getDoctrine()->getManager(); $acceptReject = $this->createForm(new AcceptRejectDealType()); $acceptReject->handleRequest($request); if ($request->isMethod('POST')) { $data = $acceptReject->getData(); /* idC -> idCreador idD -> idDestinatario idP -> idPartida idO -> idOferta */ try { $em->getRepository('BaseBundle:Ofertas')->checkDeal($data['idC'], $data['idP'], $data['idO'], $data['idD']); if ($acceptReject->get('accept')->isClicked()) { /** @var Ofertas $oferta */ $oferta = $em->getRepository('BaseBundle:Ofertas')->findOneById($data['idO']); $userPartidaLogic = new UserPartidaLogic(); $aluBlanca = $oferta->getAluBlancaIn() - $oferta->getAluBlancaOut(); $aluRoja = $oferta->getAlurojaIn() - $oferta->getAluRojaOut(); $resultado1 = $userPartidaLogic->updateBeans($data['idC'], $data['idP'], $aluRoja, $aluBlanca, $em); $aluBlanca = $oferta->getAluBlancaOut() - $oferta->getAluBlancaIn(); $aluRoja = $oferta->getAluRojaOut() - $oferta->getAlurojaIn(); $resultado2 = $userPartidaLogic->updateBeans($data['idD'], $data['idP'], $aluRoja, $aluBlanca, $em); $resultado3 = $em->getRepository('BaseBundle:Ofertas')->updateStatus(OfertaLogic::ACEPTADA, $data['idO']); $em->getRepository('BaseBundle:Log')->action2log($data['idD'], Loglogic::ACEPTAROFERTA, $data['idC']); if ($resultado1 && $resultado2 && $resultado3) { $this->get('session')->getFlashBag()->add('correct', ''); } else { $this->get('session')->getFlashBag()->add('error', ''); } } elseif ($acceptReject->get('reject')->isClicked()) { $resultado = $em->getRepository('BaseBundle:Ofertas')->updateStatus(OfertaLogic::RECHAZADA, $data['idO']); $em->getRepository('BaseBundle:Log')->action2log($data['idD'], Loglogic::RECHAZAROFERTA, $data['idC']); if ($resultado) { $this->get('session')->getFlashBag()->add('reject', ''); } } } catch (\Exception $e) { $this->get('session')->getFlashBag()->add('error', ''); } } //recoger variables de sesión $session = $this->container->get('session'); $id_partida = $session->get('id_partida'); $session->remove('id_partida'); return new RedirectResponse($this->get('router')->generate('partida_home', array('id_partida' => $id_partida))); }