Skip to content

Commit

Permalink
Merge pull request #310 from jesusantguerrero/fix/round-1
Browse files Browse the repository at this point in the history
Fix/round 1
  • Loading branch information
jesusantguerrero authored Oct 28, 2023
2 parents 4cd05d9 + c113ce9 commit 10fd79e
Show file tree
Hide file tree
Showing 38 changed files with 653 additions and 195 deletions.
4 changes: 2 additions & 2 deletions app/Domains/Journal/Actions/TransactionBulkApprove.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Gate;
use Insane\Journal\Contracts\TransactionBulkApproves;
use Insane\Journal\Models\Core\Transaction;
use Insane\Journal\Contracts\TransactionBulkApproves;

class TransactionBulkApprove implements TransactionBulkApproves
{
public function validate(User $user)
{
Gate::forUser($user)->authorize('update', Transaction::class);
Gate::forUser($user)->authorize('updateBulk', Transaction::class);
}

public function approveAllDrafts(User $user)
Expand Down
7 changes: 6 additions & 1 deletion app/Domains/Journal/Policies/TransactionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Domains\Journal\Policies;

use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Insane\Journal\Models\Core\Transaction;
use Illuminate\Auth\Access\HandlesAuthorization;

class TransactionPolicy
{
Expand All @@ -25,6 +25,11 @@ public function update(User $user, Transaction $transaction)
return $user->current_team_id == $transaction->team_id;
}

public function updateBulk(User $user)
{
return $user->current_team_id;
}

public function delete(User $user, Transaction $transaction)
{
return $user->current_team_id == $transaction->team_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace App\Domains\LogerProfile\Http\Controllers;

use App\Domains\LogerProfile\Data\LogerProfileData;
use App\Domains\LogerProfile\Services\LogerProfileService;
use App\Models\Setting;
use App\Http\Controllers\Controller;
use App\Domains\LogerProfile\Data\LogerProfileData;
use App\Http\Controllers\Traits\HasEnrichedRequest;
use App\Domains\LogerProfile\Services\LogerProfileService;

class LogerProfileController extends Controller
{
Expand All @@ -28,7 +29,6 @@ public function store(LogerProfileService $profileService)

public function show(LogerProfileService $profileService, int $profileId)
{

return inertia('LogerProfile/ProfileView', [
'profiles' => $profileService->list(auth()->user()->current_team_id),
'profile' => $profileService->getById($profileId),
Expand All @@ -37,4 +37,14 @@ public function show(LogerProfileService $profileService, int $profileId)
},
]);
}

public function transactions(int $profileId, LogerProfileService $profileService)
{
$queryParams = request()->query();

$filters = isset($queryParams['filter']) ? $queryParams['filter'] : [];
[$startDate, $endDate] = $this->getFilterDates($filters);

return $profileService->getTransactionsByProfileId($profileId, $startDate, $endDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Domains\LogerProfile\Http\Controllers;

use App\Domains\LogerProfile\Data\ProfileEntityData;
use App\Domains\LogerProfile\Services\LogerProfileService;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Traits\HasEnrichedRequest;
use App\Domains\LogerProfile\Data\ProfileEntityData;
use App\Domains\LogerProfile\Services\LogerProfileService;

class LogerProfileEntityController extends Controller
{
Expand Down
29 changes: 28 additions & 1 deletion app/Domains/LogerProfile/Services/LogerProfileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Domains\LogerProfile\Services;

use App\Domains\AppCore\Models\Category;
use App\Domains\LogerProfile\Models\LogerProfile;
use App\Domains\LogerProfile\Data\LogerProfileData;
use App\Domains\Transaction\Models\TransactionLine;
use App\Domains\LogerProfile\Data\ProfileEntityData;
use App\Domains\LogerProfile\Models\LogerProfile;
use App\Domains\LogerProfile\Models\LogerProfileEntity;
use App\Domains\Transaction\Services\TransactionService;

class LogerProfileService
{
Expand Down Expand Up @@ -40,4 +43,28 @@ public function getEntitiesByProfileId(int $profileId)
'profile_id' => $profileId,
])->get());
}

public function getTransactionsByProfileId(int $profileId, $startDate, $endDate)
{
$entities = LogerProfileEntity::where([
'profile_id' => $profileId,
'entity_type' => Category::class
])->get();

$categories = $entities->map(fn ($entity) => $entity->entity->id)->all();

$teamId = $entities[0]->team_id;

$transactions = TransactionLine::byTeam($teamId)
->inDateFrame($startDate, $endDate)
->expenseCategories($categories)
->verified()
->orderByDesc('transactions.date')
->get();

return [
"data" => $transactions,
"total" => $transactions->sum('total'),
];
}
}
3 changes: 2 additions & 1 deletion app/Domains/LogerProfile/routes.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

use Illuminate\Support\Facades\Route;
use App\Domains\LogerProfile\Http\Controllers\LogerProfileController;
use App\Domains\LogerProfile\Http\Controllers\LogerProfileEntityController;
use Illuminate\Support\Facades\Route;

Route::middleware(['auth:sanctum', 'atmosphere.teamed', 'verified'])->group(function () {
// Route::resource('/loger-profiles', [LogerProfileController::class, 'index'])->name('profiles.index');
Route::resource('/loger-profiles', LogerProfileController::class);
Route::resource('/loger-profiles/{profileId}/entities', LogerProfileEntityController::class);
Route::get('/loger-profiles/{profileId}/transactions', [LogerProfileController::class, 'transactions']);
});
55 changes: 49 additions & 6 deletions app/Domains/Transaction/Services/ReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Domains\Transaction\Services;

use App\Domains\Transaction\Models\Transaction;
use App\Domains\Transaction\Models\TransactionLine;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Domains\Transaction\Models\Transaction;
use App\Domains\Transaction\Models\TransactionLine;

class ReportService
{
Expand Down Expand Up @@ -40,12 +40,12 @@ public function revenueReport($teamId, $methodName = 'payments')
return $results;
}

public static function generateExpensesByPeriod($teamId, $timeUnit = 'month', $timeUnitDiff = 2, $type = 'expenses')
public static function generateExpensesByPeriod($teamId, $startDate, $timeUnitDiff = 2, $timeUnit = 'month')
{
$endDate = Carbon::now()->endOfMonth()->format('Y-m-d');
$startDate = Carbon::now()->subMonth($timeUnitDiff)->startOfMonth()->format('Y-m-d');
$rangeEndAt = Carbon::createFromFormat('Y-m-d', $startDate)->endOfMonth()->format('Y-m-d');
$rangeStartAt = Carbon::now()->subMonth($timeUnitDiff)->startOfMonth()->format('Y-m-d');

$results = self::getExpensesByCategoriesInPeriod($teamId, $startDate, $endDate);
$results = self::getExpensesByCategoriesInPeriod($teamId, $rangeStartAt, $rangeEndAt);
$resultGroup = $results->groupBy('date');

return $resultGroup->map(function ($monthItems) {
Expand All @@ -57,6 +57,36 @@ public static function generateExpensesByPeriod($teamId, $timeUnit = 'month', $t
}, $resultGroup)->sortBy('date');
}

public static function getIncomeVsExpenses($teamId, $timeUnitDiff = 2, $startDate = null, $timeUnit = 'month')
{
$endDate = Carbon::now()->endOfMonth()->format('Y-m-d');
$startDate = Carbon::now()->subMonth($timeUnitDiff)->startOfMonth()->format('Y-m-d');

$expenses = self::getExpensesByCategoriesInPeriod($teamId, $startDate, $endDate);
$expensesGroup = $expenses->groupBy('date');



$income = self::getIncomeByPayeeInPeriod($teamId, $startDate, $endDate);
$incomeCategories = $income->groupBy('date');

$dates = $expensesGroup->keys();


return $dates->map(function ($month) use ($incomeCategories, $expensesGroup) {
$incomeData = $incomeCategories->get($month);
$expenseData = $expensesGroup->get($month);
return [
'date' => $month,
'month_date' => $month,
'income' => $incomeData?->values()->all() ?? [],
"expense" => $expenseData?->values()->all() ?? [],
'assets' => $incomeData?->sum('total_amount') ?? 0,
'debts' => $expenseData?->sum('total') ?? 0,
];
})->sortBy('date')->values()->toArray();
}

public static function generateCurrentPreviousReport($teamId, $timeUnit = 'month', $timeUnitDiff = 2, $type = 'expenses')
{
$endDate = Carbon::now()->endOfMonth()->format('Y-m-d');
Expand Down Expand Up @@ -101,6 +131,19 @@ public static function getExpensesByYear($year, $teamId)
->get();
}

public static function getIncomeByPayeeInPeriod($teamId, $startDate, $endDate)
{
return TransactionLine::byTeam($teamId)
->balance()
->inDateFrame($startDate, $endDate)
->incomePayees()
->selectRaw('date_format(transaction_lines.date, "%Y-%m-%01") as date, payees.name, payees.id')
->groupByRaw('date_format(transaction_lines.date, "%Y-%m"), payees.id')
->orderByDesc('date')
->join('transactions', 'transactions.id', 'transaction_lines.transaction_id')
->get();
}

public static function getExpensesByCategoriesInPeriod($teamId, $startDate, $endDate, $categories = null)
{
return Transaction::byTeam($teamId)
Expand Down
15 changes: 14 additions & 1 deletion app/Domains/Transaction/Traits/TransactionLineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Domains\Transaction\Traits;

use App\Domains\Budget\Data\BudgetReservedNames;
use Illuminate\Support\Facades\DB;
use Insane\Journal\Models\Core\Transaction;
use App\Domains\Budget\Data\BudgetReservedNames;

trait TransactionLineTrait
{
Expand Down Expand Up @@ -91,6 +91,19 @@ public function scopeExpenseCategories($query, array $categories = null)
return $query;
}

public function scopeIncomePayees($query, array $payees = null)
{
$query->where('categories.name', BudgetReservedNames::READY_TO_ASSIGN->value)
->join('categories', 'transaction_lines.category_id', '=', 'categories.id')
->join('payees', 'transaction_lines.payee_id', '=', 'payees.id');

if ($payees) {
$query->whereIn('transaction_lines.payee_id', $payees);
}

return $query;
}

public function scopePayees($query, array $payees)
{
return $query->whereIn('transaction_lines.payee_id', $payees)
Expand Down
26 changes: 25 additions & 1 deletion app/Http/Controllers/Finance/FinanceTrendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FinanceTrendController extends Controller
'payees' => 'payee',
'net-worth' => 'NetWorth',
'income-expenses' => 'IncomeExpenses',
'spending-year' => 'spendingYear',
'income-expenses-graph' => 'IncomeExpensesGraph',
'year-summary' => 'yearSummary',
];
Expand Down Expand Up @@ -143,17 +144,40 @@ public function incomeExpensesGraph()
$teamId = request()->user()->current_team_id;

return [
'data' => ReportService::generateExpensesByPeriod($teamId, 'month', 12),
'data' => ReportService::getIncomeVsExpenses($teamId, 12),
'metaData' => [
'name' => 'incomeExpensesGraph',
'title' => 'Income vs Expenses',
'props' => [
'headerTemplate' => 'grid',
"assetsLabel" => "income",
"debtsLabel" => "expense"
],
],
];
}

public function spendingYear()
{
$queryParams = request()->query();
$filters = isset($queryParams['filter']) ? $queryParams['filter'] : [];
[$startDate, $endDate] = $this->getFilterDates($filters);
$teamId = request()->user()->current_team_id;

return [
'data' => ReportService::generateExpensesByPeriod($teamId, $startDate, 12),
'metaData' => [
'name' => 'spendingYear',
'title' => 'Expenses',
'props' => [
'headerTemplate' => 'grid',
],
],
];
}



public function yearSummary()
{
// $queryParams = request()->query();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/System/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __invoke()
'budgetTotal' => $budget,
'transactionTotal' => $transactionsTotal,
'expenses' => ReportService::generateCurrentPreviousReport($teamId, 'month', 1),
'revenue' => ReportService::generateExpensesByPeriod($teamId),
'revenue' => ReportService::generateExpensesByPeriod($teamId, $startDate),
'onboarding' => function () use ($team) {
$onboarding = $team->onboarding();

Expand Down
6 changes: 1 addition & 5 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ export {}

declare module 'vue' {
export interface GlobalComponents {
IClarityContractLine: typeof import('~icons/clarity/contract-line')['default']
IFluentFoodApple20Filled: typeof import('~icons/fluent/food-apple20-filled')['default']
IIcRoundQueryStats: typeof import('~icons/ic/round-query-stats')['default']
IIonEllipsisVertical: typeof import('~icons/ion/ellipsis-vertical')['default']
IMaterialSymbolsBrightnessAlertOutlineRounded: typeof import('~icons/material-symbols/brightness-alert-outline-rounded')['default']
IMaterialSymbolsHomeWorkOutline: typeof import('~icons/material-symbols/home-work-outline')['default']
IMdiBankTransfer: typeof import('~icons/mdi/bank-transfer')['default']
IMdiBankTransferIn: typeof import('~icons/mdi/bank-transfer-in')['default']
IMdiBankTransferOut: typeof import('~icons/mdi/bank-transfer-out')['default']
Expand All @@ -22,10 +20,7 @@ declare module 'vue' {
IMdiChevronRight: typeof import('~icons/mdi/chevron-right')['default']
IMdiClose: typeof import('~icons/mdi/close')['default']
IMdiEdit: typeof import('~icons/mdi/edit')['default']
IMdiEllipsis: typeof import('~icons/mdi/ellipsis')['default']
IMdiEllipsisV: typeof import('~icons/mdi/ellipsis-v')['default']
IMdiEllipsisVertical: typeof import('~icons/mdi/ellipsis-vertical')['default']
IMdiEllipsys: typeof import('~icons/mdi/ellipsys')['default']
IMdiExport: typeof import('~icons/mdi/export')['default']
IMdiFile: typeof import('~icons/mdi/file')['default']
IMdiFilter: typeof import('~icons/mdi/filter')['default']
Expand All @@ -41,5 +36,6 @@ declare module 'vue' {
IMdiStarOutline: typeof import('~icons/mdi/star-outline')['default']
IMdiSync: typeof import('~icons/mdi/sync')['default']
IMdiTrash: typeof import('~icons/mdi/trash')['default']
IMdiWallet: typeof import('~icons/mdi/wallet')['default']
}
}
Loading

0 comments on commit 10fd79e

Please sign in to comment.