Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/Admin/StatsController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API\V4\Admin; | namespace App\Http\Controllers\API\V4\Admin; | ||||
use App\Providers\PaymentProvider; | use App\Payment; | ||||
use App\User; | use App\User; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
class StatsController extends \App\Http\Controllers\Controller | class StatsController extends \App\Http\Controllers\Controller | ||||
{ | { | ||||
public const COLOR_GREEN = '#48d368'; // '#28a745' | public const COLOR_GREEN = '#48d368'; // '#28a745' | ||||
▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | protected function chartIncome(): array | ||||
// FIXME: We're using wallets.currency instead of payments.currency and payments.currency_amount | // FIXME: We're using wallets.currency instead of payments.currency and payments.currency_amount | ||||
// as I believe this way we have more precise amounts for this use-case (and default currency) | // as I believe this way we have more precise amounts for this use-case (and default currency) | ||||
$query = DB::table('payments') | $query = DB::table('payments') | ||||
->selectRaw("date_format(updated_at, '%Y-%v') as period, sum(credit_amount) as amount, wallets.currency") | ->selectRaw("date_format(updated_at, '%Y-%v') as period, sum(credit_amount) as amount, wallets.currency") | ||||
->join('wallets', 'wallets.id', '=', 'wallet_id') | ->join('wallets', 'wallets.id', '=', 'wallet_id') | ||||
->where('updated_at', '>=', $start->toDateString()) | ->where('updated_at', '>=', $start->toDateString()) | ||||
->where('status', PaymentProvider::STATUS_PAID) | ->where('status', Payment::STATUS_PAID) | ||||
->whereIn('type', [PaymentProvider::TYPE_ONEOFF, PaymentProvider::TYPE_RECURRING]) | ->whereIn('type', [Payment::TYPE_ONEOFF, Payment::TYPE_RECURRING]) | ||||
->groupByRaw('period, wallets.currency'); | ->groupByRaw('period, wallets.currency'); | ||||
$addTenantScope = function ($builder, $tenantId) { | $addTenantScope = function ($builder, $tenantId) { | ||||
$where = sprintf( | $where = sprintf( | ||||
'`wallets`.`user_id` IN (select `id` from `users` where `tenant_id` = %d)', | '`wallets`.`user_id` IN (select `id` from `users` where `tenant_id` = %d)', | ||||
$tenantId | $tenantId | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 388 Lines • Show Last 20 Lines |