Exemplo n.º 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);
 }
Exemplo n.º 2
0
 /**
  * @param FilterCriteria $filterCriteria
  *
  * @return Operation[]
  */
 public function filter(FilterCriteria $filterCriteria)
 {
     $query = $this->operationRepository->select();
     $query->where(['family_id', '=', $filterCriteria->getFamilyId()]);
     if ($filterCriteria->getCategoryId()) {
         $query->andWhere(['category_id', '=', $filterCriteria->getCategoryId()]);
     }
     if ($filterCriteria->getStartDate() || $filterCriteria->getEndDate()) {
         if ($filterCriteria->getStartDate()) {
             $startDate = date_create($filterCriteria->getStartDate())->format('Y-m-d 00:00:00');
             $query->andWhere(['create_date', '>=', $startDate]);
         }
         if ($filterCriteria->getEndDate()) {
             $endDate = date_create($filterCriteria->getEndDate())->format('Y-m-d 23:59:59');
             $query->andWhere(['create_date', '<=', $endDate]);
         }
     } elseif ($filterCriteria->getPeriod()) {
         switch ($filterCriteria->getPeriod()) {
             case FilterCriteria::FILTER_PERIOD_MONTH:
                 $period = date_create('first day of this month')->format('Y-m-d 00:00:00');
                 break;
             case FilterCriteria::FILTER_PERIOD_WEEK:
                 $period = date_create('last Monday')->format('Y-m-d 00:00:00');
                 break;
             default:
                 $period = false;
         }
         if ($period) {
             $query->andWhere(['create_date', '>=', $period]);
         }
     }
     if ($filterCriteria->getOperationType()) {
         $query->andWhere(['operation_type', '=', $filterCriteria->getOperationType()]);
     }
     if ($filterCriteria->getOrderBy()) {
         list($orderField, $order) = $filterCriteria->getOrderBy();
         $query->orderBy($orderField, $order);
     }
     $range = $filterCriteria->getRange();
     if ($range) {
         $query->limit($range->getLimit(), $range->getOffset());
     }
     return $query->all();
 }
Exemplo n.º 3
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]);
 }