Пример #1
0
 /**
  * @Route("/bill/{id}/delete", name="sb_bill_delete", requirements={"id": "\d+"})
  */
 public function deleteAction(Request $request, $id)
 {
     $bill = $this->get('app.checker')->bill($this->getUser(), $id, false);
     $budget = $bill->getBudget();
     $action = Action::deleteBill($budget, $this->getUser(), $bill->toArray());
     $em = $this->getDoctrine()->getManager();
     $em->remove($bill);
     $em->persist($action);
     $em->flush();
     $this->addFlash('notice', $this->get('translator')->trans('bill.deletesuccessful'));
     return $this->redirectToRoute('sb_budget_show', array('id' => $budget->getId()));
 }
 /**
  * @Route("/invitation/{id}/{action}", name="sb_invitation_answer", requirements={"id": "\d+", "action": "accept|refuse"})
  */
 public function answerAction($id, $action)
 {
     $invitation = $this->get('app.checker')->invitation($this->getUser(), $id, false);
     $budget = $invitation->getBudget();
     $before = $invitation->toArray();
     $actionToStatus = array('accept' => 'accepted', 'refuse' => 'refused');
     $invitation->setStatus($actionToStatus[$action]);
     $action = Action::updateInvitation($budget, $this->getUser(), $before, $invitation->toArray());
     $em = $this->getDoctrine()->getManager();
     $em->persist($invitation);
     $em->persist($action);
     $em->flush();
     $this->addFlash('notice', $this->get('translator')->trans('invitation.answersuccessful'));
     return $this->redirectToRoute('sb_budgets');
 }
Пример #3
0
 /**
  * @Route("/budget/{id}/delete", name="sb_budget_delete", requirements={"id": "\d+"})
  */
 public function deleteAction(Request $request, $id)
 {
     $budget = $this->get('app.checker')->budget($this->getUser(), $id);
     if ($request->query->get('confirm')) {
         $action = Action::deleteBudget($budget, $this->getUser(), $budget->toArray());
         $em = $this->getDoctrine()->getManager();
         $em->remove($budget);
         $em->persist($action);
         $em->flush();
         $this->addFlash('notice', $this->get('translator')->trans('budget.deletesuccessful'));
         return $this->redirectToRoute('sb_budgets');
     } else {
         return $this->render('budget/delete.html.twig', array('budget' => $budget));
     }
 }
Пример #4
0
 /**
  * @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);
 }
Пример #5
0
 public static function updateInvitation(Budget $budget, User $user, array $before, array $after)
 {
     $action = new Action();
     $action->setTemplate('invitation_update');
     $action->setBudget($budget);
     $action->setUser($user);
     $differences = array();
     if ($before['status'] != $after['status']) {
         $differences['status'] = array($before['status'], $after['status']);
     }
     $data = $after;
     $data['differences'] = $differences;
     $action->setData($data);
     return $action;
 }
Пример #6
0
 /**
  * Make a jug of this coffee
  */
 public function makeJug(Coffee $coffee)
 {
     $gramsPerJug = $coffee->getGramsPerJug();
     $gramsInStock = $coffee->getGramsInStock();
     $remaining = $gramsInStock - $gramsPerJug;
     $coffee->setGramsInStock($remaining);
     $action = new Action();
     $action->setAction(Action::MAKE_JUG)->setConsumer($this)->setCoffee($coffee)->setWhen(new \DateTime());
     $this->getActions()->add($action);
     return $this;
 }