/** * @Route("/budget/{budget_id}/bill/new", name="sb_bill_new", requirements={"budget_id": "\d+"}) */ public function newAction(Request $request, $budget_id) { $budget = $this->get('app.checker')->budget($this->getUser(), $budget_id, false); $bill = new Bill(); $bill->setBudget($budget); if (!$bill->getDate()) { $bill->setDate(new \DateTime()); } $members = $budget->getMembers(); if ($bill->getGuests()->isEmpty()) { $bill->setGuests($members); } $query = $request->query; if ($query->has('name') && $query->has('price') && $query->has('payer') && $query->has('payee')) { $bill->reimbursement($query->get('name'), $query->get('price'), $query->get('payer'), $query->get('payee')); } $form = $this->createForm(BillType::class, $bill, array('members' => $members)); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $bill->setUser($this->getUser()); $action = Action::newBill($budget, $this->getUser(), $bill->toArray()); $em = $this->getDoctrine()->getManager(); $em->persist($bill); $em->persist($action); $em->flush(); $this->addFlash('notice', $this->get('translator')->trans('bill.createsuccessful')); return $this->redirectToRoute('sb_budget_show', array('id' => $budget->getId())); } return $this->render('bill/form.html.twig', array('form' => $form->createView(), 'budget' => $budget)); }
/** * @Route("/cron", name="sb_cron") */ public function indexAction() { if (!in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) { throw $this->createAccessDeniedException("This script is only accessible from localhost."); } // find monthly bill names $em = $this->getDoctrine()->getManager(); $montlhies = $em->createQuery('SELECT DISTINCT bi.name, bu.id as budget_id FROM AppBundle:Bill bi JOIN bi.budget bu WHERE bi.monthly=TRUE ORDER BY bi.id DESC')->getResult(); // put in array indexed by budget id $budgetIndexed = array(); foreach ($montlhies as $montlhy) { $budgetId = $montlhy['budget_id']; if (!in_array($budgetId, array_keys($budgetIndexed))) { $budgetIndexed[$budgetId] = array(); } $budgetIndexed[$budgetId][] = $montlhy['name']; } // making copies $copies = array(); foreach ($budgetIndexed as $budgetId => $names) { $budget = $em->getRepository('AppBundle:Budget')->find($budgetId); foreach ($names as $name) { $lastBill = $em->createQuery('SELECT b FROM AppBundle:Bill b WHERE b.name=:name AND b.budget=:budget ORDER BY b.id DESC')->setParameter('budget', $budget)->setParameter('name', $name)->setMaxResults(1)->getSingleResult(); /* @var $lastBill Bill */ $date = $lastBill->getDate(); $now = new \DateTime(); $diff = $date->diff($now); $monthsPassed = $diff->m + $diff->y * 12; if ($monthsPassed > 0) { // copy the bill $bill = Bill::copyFrom($lastBill); $bill->setDate($now); $em->persist($bill); $copies[] = $bill->toArray(); $action = Action::copyBill($budget, $bill->toArray()); $em->persist($action); } } } $em->flush(); return new JsonResponse($copies); }