Beispiel #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;
 }
Beispiel #2
0
 public static function getConsumptionData(Xhb $xhb, \DatePeriod $period, $categoryIds)
 {
     $vehicleCostReport = new \Xhb\Model\Report\VehicleCost($xhb);
     $consumptionData = $vehicleCostReport->getPeriodConsumptionData($period, $categoryIds);
     $return = array('labels' => array(), 'datasets' => array());
     $return['datasets'][0] = array('label' => I18n::instance()->tr('Consumption'), 'strokeColor' => Output::rgbToCss(Chart::getColor(self::CONSUMPTION_CHART_COLOR_IDX)), 'pointColor' => Output::rgbToCss(Chart::getColor(self::CONSUMPTION_CHART_COLOR_IDX)), 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#bbb', 'data' => array());
     foreach ($consumptionData as $cd) {
         $return['datasets'][0]['data'][] = array('x' => $cd['date']->getTimestamp(), 'y' => $cd['per-100']);
     }
     return $return;
 }