Пример #1
0
 /**
  * @param Collection $collection
  * @param array $collectionFilters
  * @param array $accountIds
  * @return array
  */
 public static function getBalanceReportData(Xhb $xhb, array $collectionFilters, array $accountIds, $withGrandTotal = false)
 {
     $return = array('labels' => array(), 'datasets' => array());
     $operationCollection = $xhb->getOperationCollection()->setFlag('skip_aggregated_fields', true);
     $processedFilters = AccountOperation::applyFiltersOnCollection($operationCollection, $collectionFilters);
     $firstOp = $operationCollection->getFirstItem();
     if (!$firstOp) {
         return $return;
     }
     $startDate = isset($processedFilters['start_date']) ? $processedFilters['start_date'] : $firstOp->getDateModel();
     $endDate = isset($processedFilters['end_date']) ? $processedFilters['end_date'] : new \DateTime('last day of this month');
     $datePeriod = self::getDatePeriod($xhb, $startDate, $endDate);
     $rawBalanceData = array();
     foreach ($accountIds as $accountId) {
         $calculator = new Calculator($xhb, $accountId);
         $rawBalanceData[$accountId] = $calculator->getBalanceByDate($datePeriod);
     }
     $now = new \DateTime();
     $idx = 0;
     $addLabels = true;
     $grandTotal = array();
     foreach ($rawBalanceData as $accountId => $accountBalanceData) {
         $periodIdx = 0;
         if (!isset($grandTotal[$idx])) {
             $grandTotal[$idx] = array();
         }
         $return['datasets'][$idx] = array('label' => $xhb->getAccount($accountId)->getName(), 'strokeColor' => Output::rgbToCss(Chart::getColor($idx)), 'pointColor' => Output::rgbToCss(Chart::getColor($idx)), 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#bbb', 'data' => array());
         foreach ($accountBalanceData as $periodBalance) {
             if ($addLabels) {
                 $return['labels'][] = I18n::instance()->date($periodBalance['date']);
             }
             if ($periodBalance['date'] < $now) {
                 $return['datasets'][$idx]['data'][$periodIdx] = array('x' => $periodBalance['date']->getTimestamp(), 'y' => $periodBalance['balance']);
                 $grandTotal[$periodIdx][] = array('x' => $periodBalance['date'], 'y' => $periodBalance['balance']);
             } else {
                 $return['datasets'][$idx]['data'][$periodIdx] = array('x' => $periodBalance['date']->getTimestamp());
                 $grandTotal[$periodIdx][] = array('x' => $periodBalance['date']);
             }
             $periodIdx++;
         }
         $addLabels = false;
         $idx++;
     }
     if ($withGrandTotal) {
         $return['datasets'][$idx] = array('label' => I18n::instance()->tr('Grand Total'), 'strokeColor' => Output::rgbToCss(array(0, 0, 0)), 'pointColor' => Output::rgbToCss(array(0, 0, 0)), 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#bbb', 'data' => array());
         $periodIdx = 0;
         foreach ($grandTotal as $periodAccountsBalance) {
             $balance = array_sum(array_column($periodAccountsBalance, 'y'));
             $date = current($periodAccountsBalance)['x'];
             $balanceData = array('x' => $date->getTimestamp());
             if ($date < $now) {
                 $balanceData['y'] = $balance;
             }
             $return['datasets'][$idx]['data'][$periodIdx] = $balanceData;
             $periodIdx++;
         }
     }
     return $return;
 }
Пример #2
0
 public function indexAction()
 {
     $xhb = $this->getXhbSession()->getModel();
     $order = $this->getRequestQuery('order');
     $dir = $this->getRequestQuery('dir');
     if (!$order) {
         $order = 'date';
         $dir = SORT_ASC;
     }
     if (!$dir) {
         $dir = SORT_ASC;
     }
     $currentOrder = array($order => $dir);
     $coll = $this->getAccount()->getOperationCollection()->orderBy($order, $dir);
     $query = $this->getRequestQuery();
     if (!isset($query['period'])) {
         $query['period'] = Main::app()->getConfig('DEFAULT_OPERATIONS_PERIOD');
     }
     $filters = AccountOperation::applyFiltersOnCollection($coll, $query);
     // Speed up collection loading by not setting aggregated fields if not needed
     if (empty($filters['search'])) {
         $coll->setFlag('skip_aggregated_fields');
     }
     $filters = array();
     $periodFilter = new PeriodFilter($xhb, array('name' => 'period', 'id' => 'filter-period', 'value' => $query['period'], 'class' => 'filter-input'));
     $filters['period'] = $periodFilter;
     $typeFilter = new TypeFilter($xhb, array('name' => 'type', 'id' => 'filter-type', 'value' => isset($query['type']) ? $query['type'] : null, 'class' => 'filter-input'));
     $filters['type'] = $typeFilter;
     $statusFilter = new StatusFilter($xhb, array('name' => 'status', 'id' => 'filter-status', 'value' => isset($query['status']) ? $query['status'] : null, 'class' => 'filter-input'));
     $filters['status'] = $statusFilter;
     $searchFilter = new SearchFilter($xhb, array('name' => 'search', 'id' => 'filter-search', 'value' => isset($query['search']) ? $query['search'] : null, 'class' => 'filter-input'));
     $filters['search'] = $searchFilter;
     Design::instance()->addJs('chartjs/Chart.min.js')->addJs('chartjs/Chart.Scatter.js');
     //FIXME Scale is buggy with minified JS
     $this->getView()->setBlockTemplate('operation_toolbar', 'common/toolbar.phtml')->setBlockTemplate('account_summary', 'account/operation/index/summary.phtml')->setBlockTemplate('charts', 'account/operation/index/charts.phtml')->setData('OPERATION_COLLECTION', $coll)->setData('FILTER_FORM_ACTION', $this->getUrl('*/*/*'))->setData('RESET_FILTERS_URL', $this->getUrl('*/*/*'))->setData('FILTERS', $filters)->setData('CURRENT_ORDER', $currentOrder)->setData('BALANCE_REPORT_CHART', new Scatter(array('id' => 'balanceReportChart', 'title' => 'Balance Report', 'data_url' => $this->getUrl('*/balanceReportChartData/*', array('_query' => '*')), 'class' => 'toolbar-top-right', 'show_legend' => false, 'axis_type' => Scatter::AXIS_TYPE_DATE_CURRENCY)));
 }