/** * @return \DateTime */ public function getDateModel() { if (!$this->_date) { $this->_date = Date::jdToDate($this->getDate()); } return clone $this->_date; }
public function getCurrentBalance($type, $force = false, $referenceDate = 'now') { if ($force || !isset($this->_cache['balance_' . $type])) { // $txnTypeFilter = self::getBalanceStatuses($type); $referenceTime = Date::dateToJd($this->getBalanceReferenceTime($type, $referenceDate)); // $operationCollection = $this->getOperationCollection() // ->addFieldToFilter('st', array('in' => $txnTypeFilter)) // ->addFieldToFilter('date', array('le' => $referenceTime)); // // foreach($operationCollection as $o) { // $balance += $o->getAmount(); // } $balance = (double) $this->_getInitialAmount(); $balance += $this->getResource()->getCurrentBalance($this, $type, $referenceTime); $this->_cache['balance_' . $type] = $balance; } return $this->_cache['balance_' . $type]; }
/** * @param Collection $collection * @param array $collectionFilters * @return array */ public static function getTopSpendingReportData(Xhb $xhb, array $collectionFilters) { $i18n = I18n::instance(); $opColl = $xhb->getOperationCollection(); AccountOperation::applyFiltersOnCollection($opColl, $collectionFilters); $opColl->addFieldToFilter('paymode', array('neq' => Constants::PAYMODE_INTXFER))->addFieldToFilter('amount', array('lt' => 0))->addFieldToFilter('date', array('lt' => Date::dateToJd(Date::getDate()))); // FIXME does not handle split amounts yet $maxResults = 6; $sumByCategory = Chart::sumBy($opColl, 'category', 'amount', 0, $maxResults); $return = array(); $n = 0; foreach ($sumByCategory as $catId => $sum) { $cat = $xhb->getCategory($catId); if (!$cat) { $catName = $i18n->tr($n == $maxResults - 1 ? 'Other' : '(Unknown)'); } else { $catName = $cat->getFullname(); } $v = abs(round($sum, 2)); $return[] = array('value' => $v, 'label' => $i18n->tr('{0} ({1})', $catName, I18n::instance()->currency($v)), 'color' => Output::rgbToCss(Chart::getColor($n++))); } return $return; }
/** * @param AbstractCollection $collection * @param $filters * @return array */ public static function applyFiltersOnCollection(AbstractCollection $collection, $filters) { $processedFilters = array(); $xhb = $collection->getXhb(); foreach ($filters as $name => $value) { switch ($name) { case 'period': $periods = $xhb->getDateHelper()->getPredefinedTimePeriods(); $period = isset($periods[$filters['period']]) ? $periods[$filters['period']] : $periods[DateHelper::TIME_PERIOD_DEFAULT]; $ge = $period['start']; $le = $period['end']; $collection->addFieldToFilter('date', array('ge' => Date::dateToJd($ge))); $processedFilters['start_date'] = $ge; $collection->addFieldToFilter('date', array('le' => Date::dateToJd($le))); $processedFilters['end_date'] = $le; break; case 'type': switch ($value) { case 'outcome': $collection->addFieldToFilter('amount', array('lt' => 0)); $processedFilters['min_amount'] = 0; break; case 'income': $collection->addFieldToFilter('amount', array('gt' => 0)); $processedFilters['max_amount'] = 0; break; case 'any_type': default: //no filter break; } break; case 'status': switch ($value) { case 'uncategorized': $collection->addFieldToFilter('category', array('null' => true))->addFieldToFilter('scat', array('null' => true)); $processedFilters['categories'] = null; break; case 'unreconciled': $collection->addFieldToFilter('st', array('in' => Operation\Calculator::getUnreconciliedStatuses())); $processedFilters['status'] = implode(',', Operation\Calculator::getUnreconciliedStatuses()); break; case 'uncleared': $collection->addFieldToFilter('st', array('in' => Operation\Calculator::getUnclearedStatuses())); $processedFilters['status'] = implode(',', Operation\Calculator::getUnclearedStatuses()); break; case 'reconciled': $collection->addFieldToFilter('st', array('in' => Operation\Calculator::getReconciliedStatuses())); $processedFilters['status'] = implode(',', Operation\Calculator::getReconciliedStatuses()); break; case 'cleared': $collection->addFieldToFilter('st', array('in' => Operation\Calculator::getClearedStatuses())); $processedFilters['status'] = implode(',', Operation\Calculator::getClearedStatuses()); break; case 'any_status': default: //no filter break; } break; case 'search': $value = trim($value); if ($value) { $collection->addFieldToFilter('aggregate_search', array('like' => "%{$value}%")); $processedFilters['search'] = $value; } break; } } return $processedFilters; }