mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-07 19:04:41 -04:00
Fix Charts & Reports queries using SQLite
This commit is contained in:
parent
fd44ac3478
commit
23d9a2728b
@ -1,6 +1,7 @@
|
|||||||
<?php namespace App\Http\Controllers;
|
<?php namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
|
use Config;
|
||||||
use Input;
|
use Input;
|
||||||
use Utils;
|
use Utils;
|
||||||
use DB;
|
use DB;
|
||||||
@ -149,53 +150,90 @@ class ReportController extends BaseController
|
|||||||
$reportTotals['balance'][$currencyId] += $record->balance;
|
$reportTotals['balance'][$currencyId] += $record->balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($action == 'export') {
|
if ($action == 'export')
|
||||||
|
{
|
||||||
self::export($exportData, $reportTotals);
|
self::export($exportData, $reportTotals);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($enableChart) {
|
if ($enableChart)
|
||||||
foreach ([ENTITY_INVOICE, ENTITY_PAYMENT, ENTITY_CREDIT] as $entityType) {
|
{
|
||||||
$records = DB::table($entityType.'s')
|
foreach ([ENTITY_INVOICE, ENTITY_PAYMENT, ENTITY_CREDIT] as $entityType)
|
||||||
->select(DB::raw('sum(amount) as total, concat(YEAR('.$entityType.'_date), '.$groupBy.'('.$entityType.'_date)) as '.$groupBy))
|
{
|
||||||
->where('account_id', '=', Auth::user()->account_id)
|
// SQLite does not support the YEAR(), MONTH(), WEEK() and similar functions.
|
||||||
->where($entityType.'s.is_deleted', '=', false)
|
// Let's see if SQLite is being used.
|
||||||
->where($entityType.'s.'.$entityType.'_date', '>=', $startDate->format('Y-m-d'))
|
if (Config::get('database.connections.'.Config::get('database.default').'.driver') == 'sqlite')
|
||||||
->where($entityType.'s.'.$entityType.'_date', '<=', $endDate->format('Y-m-d'))
|
{
|
||||||
->groupBy($groupBy);
|
// Replace the unsupported function with it's date format counterpart
|
||||||
|
switch ($groupBy)
|
||||||
|
{
|
||||||
|
case 'MONTH':
|
||||||
|
$dateFormat = '%m'; // returns 01-12
|
||||||
|
break;
|
||||||
|
case 'WEEK':
|
||||||
|
$dateFormat = '%W'; // returns 00-53
|
||||||
|
break;
|
||||||
|
case 'DAYOFYEAR':
|
||||||
|
$dateFormat = '%j'; // returns 001-366
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$dateFormat = '%m'; // MONTH by default
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if ($entityType == ENTITY_INVOICE) {
|
// Concatenate the year and the chosen timeframe (Month, Week or Day)
|
||||||
|
$timeframe = 'strftime("%Y", '.$entityType.'_date) || strftime("'.$dateFormat.'", '.$entityType.'_date)';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Supported by Laravel's other DBMS drivers (MySQL, MSSQL and PostgreSQL)
|
||||||
|
$timeframe = 'concat(YEAR('.$entityType.'_date), '.$groupBy.'('.$entityType.'_date))';
|
||||||
|
}
|
||||||
|
|
||||||
|
$records = DB::table($entityType.'s')
|
||||||
|
->select(DB::raw('sum(amount) as total, '.$timeframe.' as '.$groupBy))
|
||||||
|
->where('account_id', '=', Auth::user()->account_id)
|
||||||
|
->where($entityType.'s.is_deleted', '=', false)
|
||||||
|
->where($entityType.'s.'.$entityType.'_date', '>=', $startDate->format('Y-m-d'))
|
||||||
|
->where($entityType.'s.'.$entityType.'_date', '<=', $endDate->format('Y-m-d'))
|
||||||
|
->groupBy($groupBy);
|
||||||
|
|
||||||
|
if ($entityType == ENTITY_INVOICE)
|
||||||
|
{
|
||||||
$records->where('is_quote', '=', false)
|
$records->where('is_quote', '=', false)
|
||||||
->where('is_recurring', '=', false);
|
->where('is_recurring', '=', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
$totals = $records->lists('total');
|
$totals = $records->lists('total');
|
||||||
$dates = $records->lists($groupBy);
|
$dates = $records->lists($groupBy);
|
||||||
$data = array_combine($dates, $totals);
|
$data = array_combine($dates, $totals);
|
||||||
|
|
||||||
$padding = $groupBy == 'DAYOFYEAR' ? 'day' : ($groupBy == 'WEEK' ? 'week' : 'month');
|
$padding = $groupBy == 'DAYOFYEAR' ? 'day' : ($groupBy == 'WEEK' ? 'week' : 'month');
|
||||||
$endDate->modify('+1 '.$padding);
|
$endDate->modify('+1 '.$padding);
|
||||||
$interval = new DateInterval('P1'.substr($groupBy, 0, 1));
|
$interval = new DateInterval('P1'.substr($groupBy, 0, 1));
|
||||||
$period = new DatePeriod($startDate, $interval, $endDate);
|
$period = new DatePeriod($startDate, $interval, $endDate);
|
||||||
$endDate->modify('-1 '.$padding);
|
$endDate->modify('-1 '.$padding);
|
||||||
|
|
||||||
$totals = [];
|
$totals = [];
|
||||||
|
|
||||||
foreach ($period as $d) {
|
foreach ($period as $d)
|
||||||
|
{
|
||||||
$dateFormat = $groupBy == 'DAYOFYEAR' ? 'z' : ($groupBy == 'WEEK' ? 'W' : 'n');
|
$dateFormat = $groupBy == 'DAYOFYEAR' ? 'z' : ($groupBy == 'WEEK' ? 'W' : 'n');
|
||||||
$date = $d->format('Y'.$dateFormat);
|
$date = $d->format('Y'.$dateFormat);
|
||||||
$totals[] = isset($data[$date]) ? $data[$date] : 0;
|
$totals[] = isset($data[$date]) ? $data[$date] : 0;
|
||||||
|
|
||||||
if ($entityType == ENTITY_INVOICE) {
|
if ($entityType == ENTITY_INVOICE)
|
||||||
|
{
|
||||||
$labelFormat = $groupBy == 'DAYOFYEAR' ? 'j' : ($groupBy == 'WEEK' ? 'W' : 'F');
|
$labelFormat = $groupBy == 'DAYOFYEAR' ? 'j' : ($groupBy == 'WEEK' ? 'W' : 'F');
|
||||||
$label = $d->format($labelFormat);
|
$label = $d->format($labelFormat);
|
||||||
$labels[] = $label;
|
$labels[] = $label;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$max = max($totals);
|
$max = max($totals);
|
||||||
|
|
||||||
if ($max > 0) {
|
if ($max > 0)
|
||||||
|
{
|
||||||
$datasets[] = [
|
$datasets[] = [
|
||||||
'totals' => $totals,
|
'totals' => $totals,
|
||||||
'colors' => $entityType == ENTITY_INVOICE ? '78,205,196' : ($entityType == ENTITY_CREDIT ? '199,244,100' : '255,107,107'),
|
'colors' => $entityType == ENTITY_INVOICE ? '78,205,196' : ($entityType == ENTITY_CREDIT ? '199,244,100' : '255,107,107'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user