Skip to content

Commit

Permalink
✨ Add new reports types
Browse files Browse the repository at this point in the history
  • Loading branch information
saleem-hadad committed Jan 15, 2022
1 parent 2492a47 commit 9a73e08
Show file tree
Hide file tree
Showing 15 changed files with 693 additions and 6 deletions.
21 changes: 21 additions & 0 deletions app/Domain/Metrics/RelationTrendMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Domain\Metrics;

abstract class RelationTrendMetric extends TrendMetric
{
protected $relationGraphqlQuery;
protected $relationDisplayUsing;
protected $relationForeignKey;

public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), [
'relation' => [
'graphql_query' => $this->relationGraphqlQuery,
'display_using' => $this->relationDisplayUsing,
'foreign_key' => $this->relationForeignKey,
]
]);
}
}
8 changes: 8 additions & 0 deletions app/Domain/Metrics/TrendMetric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Domain\Metrics;

abstract class TrendMetric extends Metric
{
protected $component = 'trend-metric';
}
16 changes: 16 additions & 0 deletions app/Domain/Ranges/LastTwelveMonths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domain\Ranges;

class LastTwelveMonths extends Range
{
public function start()
{
return now()->subMonths(12)->format("Y-m-d");
}

public function end()
{
return now()->format("Y-m-d");
}
}
42 changes: 42 additions & 0 deletions app/GraphQL/Queries/TotalExpensesTrend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\TrendMetric;
use App\Domain\Ranges\LastTwelveMonths;
use App\Domain\Ranges\CurrentYear;
use App\Domain\Ranges\LastYear;

class TotalExpensesTrend extends TrendMetric
{
public function ranges()
{
return [
new LastTwelveMonths,
new CurrentYear,
new LastYear,
];
}

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);

$query = Transaction::query()
->expenses()
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value"))
->groupBy(DB::raw("label"));

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
42 changes: 42 additions & 0 deletions app/GraphQL/Queries/TotalIncomeTrend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\TrendMetric;
use App\Domain\Ranges\LastTwelveMonths;
use App\Domain\Ranges\CurrentYear;
use App\Domain\Ranges\LastYear;

class TotalIncomeTrend extends TrendMetric
{
public function ranges()
{
return [
new LastTwelveMonths,
new CurrentYear,
new LastYear,
];
}

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);

$query = Transaction::query()
->income()
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value"))
->groupBy(DB::raw("label"));

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
49 changes: 49 additions & 0 deletions app/GraphQL/Queries/TotalPerBrandTrend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\RelationTrendMetric;
use App\Domain\Ranges\LastTwelveMonths;
use App\Domain\Ranges\CurrentYear;
use App\Domain\Ranges\LastYear;

class TotalPerBrandTrend extends RelationTrendMetric
{
protected $relationGraphqlQuery = 'allBrands';
protected $relationDisplayUsing = 'name';
protected $relationForeignKey = 'id';

public function ranges()
{
return [
new LastTwelveMonths,
new CurrentYear,
new LastYear,
];
}

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);
$brandId = $args['id'];

$query = Transaction::query()
->whereHas('brand', function ($query) use($brandId) {
return $query->where('id', $brandId);
})
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value"))
->groupBy(DB::raw("label"));

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
49 changes: 49 additions & 0 deletions app/GraphQL/Queries/TotalPerCategoryTrend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\RelationTrendMetric;
use App\Domain\Ranges\LastTwelveMonths;
use App\Domain\Ranges\CurrentYear;
use App\Domain\Ranges\LastYear;

class TotalPerCategoryTrend extends RelationTrendMetric
{
protected $relationGraphqlQuery = 'allCategories';
protected $relationDisplayUsing = 'name';
protected $relationForeignKey = 'id';

public function ranges()
{
return [
new LastTwelveMonths,
new CurrentYear,
new LastYear,
];
}

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);
$categoryId = $args['id'];

$query = Transaction::query()
->whereHas('brand.category', function ($query) use($categoryId) {
return $query->where('id', $categoryId);
})
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value"))
->groupBy(DB::raw("label"));

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
13 changes: 8 additions & 5 deletions config/finance.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use App\GraphQL\Queries\TotalPerBrand;
use App\GraphQL\Queries\IncomePerCategory;
use App\GraphQL\Queries\ExpensesPerCategory;
use App\GraphQL\Queries\TotalPerCategoryTrend;
use App\GraphQL\Queries\TotalExpensesTrend;
use App\GraphQL\Queries\TotalIncomeTrend;
use App\GraphQL\Queries\TotalPerBrandTrend;

return [
'currency' => 'AED',
Expand All @@ -18,11 +22,10 @@
new TotalExpenses,
new IncomePerCategory,
new ExpensesPerCategory,
new TotalIncomeTrend,
new TotalExpensesTrend,
new TotalPerCategoryTrend,
new TotalPerBrandTrend,
new TotalPerBrand,

// TotalIncomeTrend
// TotalExpensesTrend
// ExpensesPerCategoryTrend
// TotalPerBrandTrend
]
];
6 changes: 6 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ type Query {

totalExpenses(range: String!): Json
totalIncome(range: String!): Json

expensesPerCategory(range: String!): Json
incomePerCategory(range: String!): Json
totalPerBrand(range: String! category_id: Int): Json

totalIncomeTrend(range: String!): Json
totalExpensesTrend(range: String!): Json
totalPerCategoryTrend(range: String! id: ID!): Json
totalPerBrandTrend(range: String! id: ID!): Json
}

type Mutation {
Expand Down
12 changes: 12 additions & 0 deletions public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,21 @@ select {
.h-3 {
height: 0.75rem;
}
.h-32 {
height: 8rem;
}
.h-28 {
height: 7rem;
}
.h-24 {
height: 6rem;
}
.max-h-22 {
max-height: 5.625rem;
}
.max-h-20 {
max-height: 5rem;
}
.min-h-screen {
min-height: 100vh;
}
Expand Down
Loading

0 comments on commit 9a73e08

Please sign in to comment.