Example #1
0
 /**
  * @param int $budgetId
  * @return BudgetReport|null
  */
 public function generate($budgetId)
 {
     $budget = $this->budgetRepository->getBudgetById($budgetId);
     if (!$budget) {
         // @todo need throw exception
         return null;
     }
     $user = $this->userRepository->getUserById($budget->getCreateUserId());
     if (!$user) {
         // @todo need throw exception
         return null;
     }
     $estimates = $this->estimateRepository->getEstimatesByBudgetId($budget->getBudgetId());
     $categoryIds = [];
     foreach ($estimates as $estimate) {
         $categoryIds[] = $estimate->getCategoryId();
     }
     $budgetEstimates = [];
     if (!empty($categoryIds)) {
         $categories = $this->categoryRepository->getListOfCategoriesByIds($categoryIds);
         foreach ($estimates as $estimate) {
             if (empty($categories[$estimate->getCategoryId()])) {
                 // @todo need throw exception
                 continue;
             }
             $category = $categories[$estimate->getCategoryId()];
             $filterCriteria = new FilterCriteria();
             $filterCriteria->setFamilyId($budget->getFamilyId());
             $filterCriteria->setCategoryId($category->getCategoryId());
             $filterCriteria->setOperationType(Operation::OPERATION_TYPE_EXPENSE);
             $filterCriteria->setStartDate($budget->getStartDate());
             $filterCriteria->setEndDate($budget->getEndDate());
             $budgetEstimates[] = new BudgetEstimate($estimate, $category, $this->operationFilter->filter($filterCriteria));
         }
     }
     return new BudgetReport($budget, $user, $budgetEstimates);
 }
Example #2
0
 /**
  * Выводит список расходов/доходов для управления.
  *
  * @param Request $request
  *
  * @return string Список расходов/доходов.
  */
 public function listAction(Request $request)
 {
     if (!$this->getUser()->isAuth()) {
         $this->redirect('user/login');
     }
     $filterCriteria = new FilterCriteria();
     $filterCriteria->setOrderBy('operation_id', OrderByExpressionInterface::ORDER_DESC);
     /** @var FamilyRepository $familyRepository */
     $familyRepository = $this->getDbFacade()->getEntityRepository(FamilyRepository::class);
     $family = $familyRepository->getFamilyByUserId($this->getUser()->getUserId());
     $filterCriteria->setFamilyId($family->getFamilyId());
     if ($request->hasRequest('operationFilter')) {
         $filterCriteria->setApplyFilter(true);
         $operationFilter = $request->getRequest('operationFilter');
         if (isset($operationFilter['categoryId'])) {
             $filterCriteria->setCategoryId($operationFilter['categoryId']);
         }
         if (isset($operationFilter['period'])) {
             $filterCriteria->setPeriod($operationFilter['period']);
         }
         if (isset($operationFilter['operationType'])) {
             $filterCriteria->setOperationType($operationFilter['operationType']);
         }
         if (isset($operationFilter['startDate'])) {
             $filterCriteria->setStartDate($operationFilter['startDate']);
         }
         if (isset($operationFilter['endDate'])) {
             $filterCriteria->setEndDate($operationFilter['endDate']);
         }
     }
     /** @var OperationRepository $operationRepository */
     $operationRepository = $this->getDbFacade()->getEntityRepository(OperationRepository::class);
     $paginator = new Paginator(new PaginationFactory(), new PaginationViewer($this->view));
     $pagination = null;
     try {
         $pagination = $paginator->build(self::SHOW_OPERATIONS_ON_PER_PAGE, $operationRepository->getOperationsCountByFamilyId($family->getFamilyId()), (int) $request->getQuery('page'));
         $filterCriteria->setRange(new RangeStruct($pagination->getPerPage(), $pagination->getOffset()));
     } catch (\InvalidArgumentException $exc) {
         // @todo Need add logging
     }
     $operationFilterService = new OperationFilter($operationRepository);
     $operations = $operationFilterService->filter($filterCriteria);
     /** @var UserRepository $userRepository */
     $userRepository = $this->getDbFacade()->getEntityRepository(UserRepository::class);
     $familyManager = new FamilyManager($familyRepository, $userRepository);
     $users = $familyManager->getUsersOfFamilyByUserId($this->getUser()->getUserId());
     /** @var CategoryRepository $categoryRepository */
     $categoryRepository = $this->getDbFacade()->getEntityRepository(CategoryRepository::class);
     $categories = $categoryRepository->getListOfCategoriesByFamilyId($family->getFamilyId());
     return $this->view->render('operation/list', ['users' => $users, 'filterCriteria' => $filterCriteria, 'categories' => $categories, 'operations' => $operations, 'pagination' => $pagination]);
 }