Files
project/app/View/Composers/MonthGraph.php

42 lines
1.3 KiB
PHP

<?php
namespace App\View\Composers;
use App\Models\Incoming;
use App\Models\Invoice;
use Illuminate\Support\Carbon;
use Illuminate\View\View;
class MonthGraph
{
public function compose(View $view): void
{
$monthly_invoices = Invoice::whereYear('created_at', '=', Carbon::now()->year)->get()
->groupBy(function ($invoice) {
return $invoice->created_at->format('n');
})
->map(function ($month) {
return $month->sum('sum');
});
$monthly_incoming = Incoming::whereYear('issue_date', '=', Carbon::now()->year)->get()
->groupBy(function ($incoming) {
return Carbon::parse($incoming->issue_date)->format('n');
})
->map(function ($month) {
return $month->sum('net');
});
$diff_invoices = $monthly_invoices->diffKeys($monthly_incoming);
foreach ($diff_invoices as $year => $invoices) {
$monthly_incoming[$year] = 0;
}
$diff_incoming = $monthly_incoming->diffKeys($monthly_invoices);
foreach ($diff_incoming as $year => $invoices) {
$monthly_invoices[$year] = 0;
}
$view->with(['monthly_invoices' => $monthly_invoices, 'monthly_incoming' => $monthly_incoming]);
}
}