Skip to content

Commit

Permalink
Merge pull request #323 from jesusantguerrero/fix/calulations
Browse files Browse the repository at this point in the history
Fix/calulations
  • Loading branch information
jesusantguerrero authored Nov 30, 2023
2 parents 5cfa2b9 + 739d119 commit b218088
Show file tree
Hide file tree
Showing 34 changed files with 387 additions and 389 deletions.
31 changes: 11 additions & 20 deletions app/Console/Commands/TeamBudgetRollover.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Data\BudgetReservedNames;
use App\Domains\Budget\Services\BudgetRolloverService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class TeamBudgetRollover extends Command
{
Expand All @@ -15,7 +15,7 @@ class TeamBudgetRollover extends Command
*
* @var string
*/
protected $signature = 'app:team-budget-rollover {teamId}';
protected $signature = 'app:team-budget-rollover {teamId} ?{date}';

/**
* The console command description.
Expand All @@ -31,26 +31,17 @@ public function handle(BudgetRolloverService $rolloverService)
{

$teamId = $this->argument('teamId');

$categories = Category::where([
'team_id' => $teamId,
])
->whereNot('name', BudgetReservedNames::READY_TO_ASSIGN->value)
->get();
$date = $this->argument('date');

$monthsWithTransactions = DB::table('transaction_lines')
->where([
"team_id" => $teamId
])
->selectRaw("date_format(transaction_lines.date, '%Y-%m') AS date")
->groupBy(DB::raw("date_format(transaction_lines.date, '%Y-%m')"))
->get()
->pluck('date');

$total = count($monthsWithTransactions);
$count = 0;
foreach ($monthsWithTransactions as $month) {
$count++;
$rolloverService->rollMonth($teamId, $month."-01", $categories);
echo "updated month {$month}".PHP_EOL;
echo "{$count} of {$total}".PHP_EOL.PHP_EOL;
}
->orderBy('date')
->first();

$rolloverService->startFrom($teamId, $date ?? $monthsWithTransactions->date);
}
}
25 changes: 15 additions & 10 deletions app/Domains/Budget/Http/Controllers/BudgetCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Http\Resources\CategoryGroupCollection;
use Freesgen\Atmosphere\Http\InertiaController;
use App\Domains\Transaction\Services\ReportService;
use App\Domains\Budget\Services\BudgetTargetService;
use App\Domains\Budget\Services\BudgetAccountService;
use App\Domains\Budget\Services\BudgetCategoryService;

Expand Down Expand Up @@ -99,24 +100,28 @@ public function show(int $categoryId)
}

// Category Budget targets
public function addCategoryBudget(Request $request, $categoryId)
public function addCategoryBudget(Request $request, $categoryId, BudgetTargetService $budgetTargetService)
{
$category = Category::find($categoryId);
$postData = $request->post();
$category->budget()->create(array_merge($postData, [
'name' => $category->name,
'user_id' => $request->user()->id,
'team_id' => $request->user()->current_team_id,
]));

$budgetTargetService->add(
$category,
request()->user(),
$request->post()
);

return Redirect::back();
}

public function updateCategoryBudget(Request $request, $categoryId)
public function updateCategoryBudget($categoryId, BudgetTargetService $budgetTargetService)
{
$category = Category::find($categoryId);
$postData = $request->post();
$category->budget->update($postData);
$budgetTargetService->update(
$category,
$category->budget,
request()->user(),
request()->post()
);

return Redirect::back();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function assign(BudgetMovementService $service, Category $category, strin
{
$isMovement = request()->post('type');
$postData = $this->getPostData();
if (! $isMovement && $postData && $postData['budgeted'] != null) {
if (!$isMovement && $postData && $postData['budgeted'] != null) {
$service->registerAssignment(new BudgetAssignData(
$postData['team_id'],
$postData['user_id'],
Expand Down
16 changes: 4 additions & 12 deletions app/Domains/Budget/Http/Controllers/BudgetTargetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,20 @@
use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Models\BudgetTarget;
use App\Domains\Budget\Services\BudgetTargetService;
use App\Domains\Budget\Services\BudgetCategoryService;

class BudgetTargetController extends Controller
{
public function store(Category $category, BudgetCategoryService $service)
public function store(Category $category, BudgetTargetService $budgetTargetService)
{
$postData = request()->post();
$service->addTarget($category, $postData);

$budgetTargetService->add($category, request()->user(), $postData);
return redirect()->back();
}

public function update(Category $category, BudgetTarget $budgetTarget)
public function update(Category $category, BudgetTarget $budgetTarget, BudgetTargetService $budgetTargetService)
{
$postData = request()->post();
$budgetTarget->update(array_merge(
$postData, [
'team_id' => request()->user()->current_team_id,
'name' => $category->name,
'category_id' => $budgetTarget->category_id,
]));

$budgetTargetService->update($category, $budgetTarget, request()->user(), $postData);
return redirect()->back();
}

Expand Down
10 changes: 5 additions & 5 deletions app/Domains/Budget/Models/BudgetMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace App\Domains\Budget\Models;

use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Data\BudgetReservedNames;
use DateTime;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Insane\Journal\Models\Core\Account;
use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Data\BudgetReservedNames;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class BudgetMonth extends Model
{
Expand All @@ -27,7 +27,7 @@ class BudgetMonth extends Model
'funded_spending',
'payments',
'left_from_last_month',
'funded_spending_previous_month',
'overspending_previous_month',
];

public function category()
Expand Down
22 changes: 21 additions & 1 deletion app/Domains/Budget/Models/BudgetMovement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace App\Domains\Budget\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Domains\AppCore\Models\Category;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class BudgetMovement extends Model
{
Expand All @@ -17,4 +18,23 @@ class BudgetMovement extends Model
'amount',
'date',
];

public function source()
{
return $this->belongsTo(Category::class, 'source_category_id');
}

public function destination()
{
return $this->belongsTo(Category::class, 'destination_category_id');
}

protected static function booted()
{

static::saving(function ($movement) {
$movement->destination_category_name = $movement->destination->name;
$movement->source_category_name = $movement->source->name;
});
}
}
5 changes: 3 additions & 2 deletions app/Domains/Budget/Models/BudgetTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ public function getExpensesByPeriod($startDate, $endDate = null)
->get()[0]->total;
}

public static function getNextTargets($teamId)
public static function getNextTargets($teamId, $targetTypes = ['spending'])
{
return DB::query()
->whereIn('budget_targets.target_type', ['spending'])
->whereIn('budget_targets.target_type', $targetTypes)
->where([
'frequency' => 'monthly',
'budget_targets.team_id' => $teamId,
])
->whereRaw("concat(date_format(now(), '%Y-%m'), '-', frequency_month_date) >= now()")
->addSelect(DB::raw("budget_targets.*, concat(date_format(now(), '%Y-%m'), '-', frequency_month_date) as due_date"))
->from('budget_targets')
->get();
}
Expand Down
Loading

0 comments on commit b218088

Please sign in to comment.