Skip to content

Commit

Permalink
Merge pull request #302 from jesusantguerrero/release/stable
Browse files Browse the repository at this point in the history
Release/stable
  • Loading branch information
jesusantguerrero authored Oct 16, 2023
2 parents 58b05a1 + 5b7ab8a commit bc1a9be
Show file tree
Hide file tree
Showing 62 changed files with 1,050 additions and 657 deletions.
4 changes: 2 additions & 2 deletions app/Console/Commands/CreatePlannedTransactionsFromBudget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Console\Commands;

use App\Domains\Budget\Services\BudgetTargetTransactionService;
use App\Domains\Budget\Services\BudgetTargetService;
use App\Models\Team;
use Illuminate\Console\Command;

Expand All @@ -12,7 +12,7 @@ class CreatePlannedTransactionsFromBudget extends Command

protected $description = 'Create planned transactions from budget.';

public function handle(BudgetTargetTransactionService $service): mixed
public function handle(BudgetTargetService $service): mixed
{
$team = Team::find($this->argument('teamId'));
$service->createPlannedTransactions($team->id);
Expand Down
6 changes: 3 additions & 3 deletions app/Console/Commands/MakeOccurrenceReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Console\Commands;

use App\Domains\Housing\Contracts\OccurrenceNotifyTypes;
use App\Domains\Housing\Models\OccurrenceCheck;
use App\Domains\Housing\Models\Occurrence;
use App\Models\User;
use App\Notifications\OccurrenceAlert;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -31,8 +31,8 @@ class MakeOccurrenceReminders extends Command
*/
public function handle()
{
$occurrencesOnLast = OccurrenceCheck::getForNotificationType(OccurrenceNotifyTypes::LAST);
$occurrencesOnAvg = OccurrenceCheck::getForNotificationType(OccurrenceNotifyTypes::AVG);
$occurrencesOnLast = Occurrence::getForNotificationType(OccurrenceNotifyTypes::LAST);
$occurrencesOnAvg = Occurrence::getForNotificationType(OccurrenceNotifyTypes::AVG);

$this->sendNotifications($occurrencesOnLast);
$this->sendNotifications($occurrencesOnAvg);
Expand Down
9 changes: 6 additions & 3 deletions app/Domains/AppCore/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace App\Domains\AppCore\Models;

use App\Models\Team;
use App\Domains\Budget\Models\BudgetMonth;
use App\Domains\Budget\Models\BudgetTarget;
use App\Models\Team;
use Insane\Journal\Models\Core\Category as CoreCategory;
use App\Domains\Budget\Models\Traits\BudgetCategoryTrait;

class Category extends CoreCategory
{
use BudgetCategoryTrait;

protected $with = ['budget'];

public function team()
Expand All @@ -23,7 +26,7 @@ public function budget()

public function budgets()
{
return $this->hasMany(BudgetMonth::class)->orderBy('month', 'desc');
return $this->hasMany(BudgetMonth::class)->orderBy('month', 'desc')->limit(2);
}

public function subCategories()
Expand All @@ -33,6 +36,6 @@ public function subCategories()

public function lastMonthBudget()
{
return $this->hasMany(BudgetMonth::class)->orderBy('month', 'desc')->limit(1);
return $this->hasOne(BudgetMonth::class)->orderBy('month', 'desc')->limit(1);
}
}
16 changes: 8 additions & 8 deletions app/Domains/Automation/Services/LogerAutomationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Domains\Automation\Services;

use App\Domains\Automation\Models\Automation;
use App\Domains\Automation\Models\AutomationTask;
use App\Domains\Integration\Actions\BHD;
use App\Domains\Automation\Models\Automation;
use App\Domains\Integration\Actions\BHDAlert;
use App\Domains\Integration\Actions\OccurrenceCheckAutomation;
use App\Domains\Automation\Models\AutomationTask;
use App\Domains\Integration\Actions\TransactionCreateEntry;
use App\Domains\Integration\Actions\OccurrenceAutomation;

class LogerAutomationService
{
Expand Down Expand Up @@ -276,18 +276,18 @@ public static function services()
],
'type' => 'internal',
],
'occurrenceChecks' => [
'Occurrences' => [
'name' => 'occurrence_check',
'label' => 'Occurrence Check',
'logo' => '/images/meal-planner.png',
'entity' => OccurrenceCheckAutomation::class,
'entity' => OccurrenceAutomation::class,
'description' => 'Occurrence check',
'fields' => json_encode(OccurrenceCheckAutomation::fieldConfig()),
'fields' => json_encode(OccurrenceAutomation::fieldConfig()),
'actions' => [
[
'name' => 'createOccurrenceCheck',
'name' => 'createOccurrence',
'label' => 'Create Occurrence Check',
'entity' => OccurrenceCheckAutomation::class,
'entity' => OccurrenceAutomation::class,
'description' => 'Create an occurrence check',
'config' => json_encode([
'name' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

namespace App\Http\Controllers\Finance;
namespace App\Domains\Budget\Http\Controllers;

use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Domains\AppCore\Models\Category;
use Illuminate\Support\Facades\Redirect;
use App\Domains\Budget\Models\BudgetMovement;
use App\Domains\Budget\Services\BudgetAccountService;
use App\Domains\Budget\Services\BudgetCategoryService;
use App\Domains\Transaction\Models\Transaction;
use App\Domains\Transaction\Services\ReportService;
use App\Http\Resources\CategoryGroupCollection;
use App\Models\Setting;
use Freesgen\Atmosphere\Http\InertiaController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Validation\Rule;
use App\Domains\Transaction\Services\ReportService;
use App\Domains\Budget\Services\BudgetAccountService;
use App\Domains\Budget\Services\BudgetCategoryService;

class BudgetCategoryController extends InertiaController
{
Expand All @@ -34,27 +34,33 @@ public function __construct(Category $category, public BudgetAccountService $acc
'name' => 'required|string|max:255|unique:categories',
];
$this->sorts = ['index'];
$this->includes = ['subCategories', 'subCategories.budget', 'subCategories.budgets'];
$this->includes = [];
$this->filters = [
'parent_id' => '$null',
'resource_type' => 'transactions',
];
$this->resourceName = 'budgets';
}

protected function getIndexProps(Request $request, $resources = null): array
{
protected function index(Request $request) {
$resourceName = $this->resourceName ?? $this->model->getTable();
$queryParams = request()->query();
$settings = Setting::getByTeam(auth()->user()->current_team_id);
$timeZone = $settings['team_timezone'] ?? config('app.timezone');
$filters = isset($queryParams['filter']) ? $queryParams['filter'] : [];
[$startDate, $endDate] = $this->getFilterDates($filters, $timeZone);

$accountTotalBalance = $this->accountService->getBalanceAs($request->user()->current_team_id, $endDate);
$resources = $this->parser($this->getModelQuery($request, null, function ($model) use ($startDate) {
return $model->withCurrentSavings($startDate);
}));

return [
'accountTotal' => $accountTotalBalance,
];

return inertia($this->templates['index'],
[
$resourceName => $this->parser($resources),
"serverSearchOptions" => $this->getServerParams(),
"accountTotal" => $this->accountService->getBalanceAs($request->user()->current_team_id, $endDate)
]);
}

public function show(int $categoryId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\Finance;
namespace App\Domains\Budget\Http\Controllers;

use App\Domains\Budget\Models\Budget;
use Freesgen\Atmosphere\Http\InertiaController;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

namespace App\Http\Controllers\Finance;
namespace App\Domains\Budget\Http\Controllers;

use App\Domains\Budget\Data\BudgetAssignData;
use App\Domains\Budget\Data\BudgetMovementData;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Redirect;
use Insane\Journal\Models\Core\Category;
use App\Domains\Budget\Exports\BudgetExport;
use App\Domains\Budget\Imports\BudgetImport;
use App\Domains\Budget\Data\BudgetAssignData;
use App\Domains\Budget\Data\BudgetMovementData;
use App\Http\Controllers\Traits\HasEnrichedRequest;
use App\Domains\Budget\Services\BudgetCategoryService;
use App\Domains\Budget\Services\BudgetMovementService;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Traits\HasEnrichedRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Insane\Journal\Models\Core\Category;
use Maatwebsite\Excel\Facades\Excel;

class BudgetMonthController extends Controller
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace App\Http\Controllers\Finance;
namespace App\Domains\Budget\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Models\BudgetTarget;
use App\Domains\Budget\Services\BudgetTargetService;
use App\Domains\Budget\Services\BudgetCategoryService;
use App\Http\Controllers\Controller;

class BudgetTargetController extends Controller
{
Expand All @@ -29,4 +30,11 @@ public function update(Category $category, BudgetTarget $budgetTarget)

return redirect()->back();
}

public function complete(Category $category, BudgetTarget $budgetTarget, BudgetTargetService $budgetTargetService)
{
$postData = request()->post();
$budgetTargetService->complete($budgetTarget, $category, $postData);
return redirect()->back();
}
}
18 changes: 15 additions & 3 deletions app/Domains/Budget/Models/BudgetTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

namespace App\Domains\Budget\Models;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use App\Domains\Transaction\Models\Transaction;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BudgetTarget extends Model
{
use HasFactory;

protected $fillable = ['team_id', 'user_id', 'color', 'amount', 'name', 'target_type', 'frequency', 'frequency_date', 'frequency_week_day', 'frequency_month_date'];
protected $fillable = [
'team_id',
'user_id',
'color',
'amount',
'name',
'target_type',
'frequency',
'frequency_date',
'frequency_week_day',
'frequency_month_date',
'completed_at'
];

public function getExpensesByPeriod($startDate, $endDate = null)
{
Expand Down
20 changes: 20 additions & 0 deletions app/Domains/Budget/Models/Traits/BudgetCategoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace App\Domains\Budget\Models\Traits;

trait BudgetCategoryTrait {
public function scopeWithCurrentSavings($query, $startDate) {
return $query->with(['subCategories' => fn ($q) => $q->whereHas('budget', function($query) use ($startDate) {
return $query->whereRaw('(completed_at is null or completed_at > ?)', $startDate);
})->orWhereDoesntHave('budget')]);
}

/***
* here will the filters for:
* go funded
* not funded
* credit cards
* savings or goals
* */


}
30 changes: 21 additions & 9 deletions app/Domains/Budget/Services/BudgetCategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace App\Domains\Budget\Services;

use App\Domains\Budget\Data\BudgetReservedNames;
use App\Domains\Budget\Data\CategoryData;
use App\Domains\Budget\Models\BudgetMonth;
use App\Domains\Budget\Models\BudgetTarget;
use App\Events\BudgetAssigned;
use App\Helpers\RequestQueryHelper;
use Brick\Money\Money;
use Illuminate\Support\Str;
use App\Events\BudgetAssigned;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use App\Helpers\RequestQueryHelper;
use Insane\Journal\Models\Core\Account;
use Insane\Journal\Models\Core\Category;
use App\Domains\Budget\Data\CategoryData;
use App\Domains\Budget\Models\BudgetMonth;
use App\Domains\Budget\Models\BudgetTarget;
use App\Domains\Budget\Data\BudgetReservedNames;

class BudgetCategoryService
{
Expand All @@ -36,9 +36,8 @@ public function updateTarget(Category $category, mixed $postData)
]));
}

public static function getSavings($teamId, $startDate, $endDate)
public static function getSavingsBalance($teamId, $startDate, $endDate)
{
$startMonth = substr((string) $startDate, 0, 7);
$endMonth = substr((string) $endDate, 0, 7);

return DB::query()
Expand All @@ -50,6 +49,19 @@ public static function getSavings($teamId, $startDate, $endDate)
->sum(DB::raw('budgeted + activity'));
}

public function getLastTransactionMonth($category)
{
return DB::query()
->where('budget_targets.team_id', $category->team_id)
->where('budget_targets.category_id', $category->id)
->where('activity', '<>', 0)
->whereIn('budget_targets.target_type', ['saving_balance'])
->from('budget_months')
->join('budget_targets', 'budget_targets.category_id', 'budget_months.category_id')
->orderByDesc('month')
->first();
}

public function assignBudget(Category $category, string $month, mixed $postData)
{
$amount = (float) $postData['budgeted'];
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Budget/Services/BudgetMonthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function updateTarget(Category $category, mixed $postData)
]));
}

public static function getSavings($teamId, $startDate, $endDate)
public static function getSavingsBalance($teamId, $startDate, $endDate)
{
$startMonth = substr((string) $startDate, 0, 7);
$endMonth = substr((string) $endDate, 0, 7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace App\Domains\Budget\Services;

use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Models\BudgetTarget;
use App\Domains\Integration\Concerns\PlannedTransactionDTO;
use App\Domains\Transaction\Services\PlannedTransactionService;

class BudgetTargetTransactionService
class BudgetTargetService
{
public function __construct(private PlannedTransactionService $plannedService)
public function __construct(private PlannedTransactionService $plannedService, private BudgetCategoryService $budgetCategoryService)
{

}
Expand Down Expand Up @@ -36,4 +37,13 @@ private function buildPlanned(BudgetTarget $target, $month)
$data = PlannedTransactionDTO::fromTarget($target, $date);
$this->plannedService->add($data);
}

public function complete(BudgetTarget $budgetTarget, Category $category, array $postData) {
$budgetTarget->update(array_merge(
$postData, [
'completed_at' => $postData['completed_at']
?? $this->budgetCategoryService->getLastTransactionMonth($category)?->month,
]));

}
}
Loading

0 comments on commit bc1a9be

Please sign in to comment.