Files
InvoiceShelf/app/Http/Controllers/V1/Admin/Dashboard/DashboardController.php
mchev 3259173066 Laravel 11 (#84)
* Convert string references to `::class`

PHP 5.5.9 adds the new static `class` property which provides the fully qualified class name. This is preferred over using strings for class names since the `class` property references are checked by PHP.

* Use Faker methods

Accessing Faker properties was deprecated in Faker 1.14.

* Convert route options to fluent methods

Laravel 8 adopts the tuple syntax for controller actions. Since the old options array is incompatible with this syntax, Shift converted them to use modern, fluent methods.

* Adopt class based routes

* Remove default `app` files

* Shift core files

* Streamline config files

* Set new `ENV` variables

* Default new `bootstrap/app.php`

* Re-register HTTP middleware

* Consolidate service providers

* Re-register service providers

* Re-register routes

* Re-register scheduled commands

* Bump Composer dependencies

* Use `<env>` tags for configuration

`<env>` tags have a lower precedence than system environment variables making it easier to overwrite PHPUnit configuration values in additional environments, such a CI.

Review this blog post for more details on configuration precedence when testing Laravel: https://jasonmccreary.me/articles/laravel-testing-configuration-precedence/

* Adopt anonymous migrations

* Rename `password_resets` table

* Convert `$casts` property to method

* Adopt Laravel type hints

* Mark base controller as `abstract`

* Remove `CreatesApplication` testing trait

* Shift cleanup

* Fix shift first issues

* Updating Rules for laravel 11, sanctum config and pint

* Fix Carbon issue on dashboard

* Temporary fix for tests while migration is issue fixed on laravel side

* Carbon needs numerical values, not strings

* Minimum php version

* Fix domain installation step not fetching the correct company_id

* Fix Role Policy wasn't properly registered

---------
2024-06-05 11:33:52 +02:00

167 lines
5.5 KiB
PHP

<?php
namespace App\Http\Controllers\V1\Admin\Dashboard;
use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\CompanySetting;
use App\Models\Customer;
use App\Models\Estimate;
use App\Models\Expense;
use App\Models\Invoice;
use App\Models\Payment;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Silber\Bouncer\BouncerFacade;
class DashboardController extends Controller
{
/**
* Handle the incoming request.
*
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke(Request $request)
{
$company = Company::find($request->header('company'));
$this->authorize('view dashboard', $company);
$invoice_totals = [];
$expense_totals = [];
$receipt_totals = [];
$net_income_totals = [];
$i = 0;
$months = [];
$monthCounter = 0;
$fiscalYear = CompanySetting::getSetting('fiscal_year', $request->header('company'));
$startDate = Carbon::now();
$start = Carbon::now();
$end = Carbon::now();
$terms = explode('-', $fiscalYear);
$companyStartMonth = intval($terms[0]);
if ($companyStartMonth <= $start->month) {
$startDate->month($companyStartMonth)->startOfMonth();
$start->month($companyStartMonth)->startOfMonth();
$end->month($companyStartMonth)->endOfMonth();
} else {
$startDate->subYear()->month($companyStartMonth)->startOfMonth();
$start->subYear()->month($companyStartMonth)->startOfMonth();
$end->subYear()->month($companyStartMonth)->endOfMonth();
}
if ($request->has('previous_year')) {
$startDate->subYear()->startOfMonth();
$start->subYear()->startOfMonth();
$end->subYear()->endOfMonth();
}
while ($monthCounter < 12) {
array_push(
$invoice_totals,
Invoice::whereBetween(
'invoice_date',
[$start->format('Y-m-d'), $end->format('Y-m-d')]
)
->whereCompany()
->sum('base_total')
);
array_push(
$expense_totals,
Expense::whereBetween(
'expense_date',
[$start->format('Y-m-d'), $end->format('Y-m-d')]
)
->whereCompany()
->sum('base_amount')
);
array_push(
$receipt_totals,
Payment::whereBetween(
'payment_date',
[$start->format('Y-m-d'), $end->format('Y-m-d')]
)
->whereCompany()
->sum('base_amount')
);
array_push(
$net_income_totals,
($receipt_totals[$i] - $expense_totals[$i])
);
$i++;
array_push($months, $start->format('M'));
$monthCounter++;
$end->startOfMonth();
$start->addMonth()->startOfMonth();
$end->addMonth()->endOfMonth();
}
$start->subMonth()->endOfMonth();
$total_sales = Invoice::whereBetween(
'invoice_date',
[$startDate->format('Y-m-d'), $start->format('Y-m-d')]
)
->whereCompany()
->sum('base_total');
$total_receipts = Payment::whereBetween(
'payment_date',
[$startDate->format('Y-m-d'), $start->format('Y-m-d')]
)
->whereCompany()
->sum('base_amount');
$total_expenses = Expense::whereBetween(
'expense_date',
[$startDate->format('Y-m-d'), $start->format('Y-m-d')]
)
->whereCompany()
->sum('base_amount');
$total_net_income = (int) $total_receipts - (int) $total_expenses;
$chart_data = [
'months' => $months,
'invoice_totals' => $invoice_totals,
'expense_totals' => $expense_totals,
'receipt_totals' => $receipt_totals,
'net_income_totals' => $net_income_totals,
];
$total_customer_count = Customer::whereCompany()->count();
$total_invoice_count = Invoice::whereCompany()
->count();
$total_estimate_count = Estimate::whereCompany()->count();
$total_amount_due = Invoice::whereCompany()
->sum('base_due_amount');
$recent_due_invoices = Invoice::with('customer')
->whereCompany()
->where('base_due_amount', '>', 0)
->take(5)
->latest()
->get();
$recent_estimates = Estimate::with('customer')->whereCompany()->take(5)->latest()->get();
return response()->json([
'total_amount_due' => $total_amount_due,
'total_customer_count' => $total_customer_count,
'total_invoice_count' => $total_invoice_count,
'total_estimate_count' => $total_estimate_count,
'recent_due_invoices' => BouncerFacade::can('view-invoice', Invoice::class) ? $recent_due_invoices : [],
'recent_estimates' => BouncerFacade::can('view-estimate', Estimate::class) ? $recent_estimates : [],
'chart_data' => $chart_data,
'total_sales' => $total_sales,
'total_receipts' => $total_receipts,
'total_expenses' => $total_expenses,
'total_net_income' => $total_net_income,
]);
}
}