Example #1
0
 /**
  * @param int $familyId
  *
  * @return int
  */
 public function getIncomeAmountInMonth($familyId)
 {
     $query = $this->operationRepository->prepare(sprintf("select sum(amount) from operation where family_id=:family_id and operation_type='income' and create_date >= '%s'", date_create('first day of this month')->format('Y-m-d 00:00:00')));
     $query->bindValue(':family_id', $familyId);
     if (!$query->execute()) {
         return 0;
     }
     return (int) $query->fetchColumn();
 }
Example #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();
 }