mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-01 12:51:00 +00:00
* refactor: stabilize model identities for domain migration * refactor: extract module platform context * refactor: assign models to domain contexts * refactor: extract ai platform context * refactor: extract storage platform context * refactor: extract mail platform context * refactor: extract pdf platform context * refactor: extract operations platform context * refactor: move installation into operations platform * refactor: extract money domain context * refactor: extract taxation domain context * refactor: extract catalog domain context * refactor: extract metadata domain context * refactor: extract reporting domain context * refactor: extract purchases domain context * refactor: extract receivables domain context * refactor: extract accounts domain context * refactor: complete reporting statement boundary * refactor: extract contacts domain context * refactor: extract sales domain context * refactor: remove legacy application layers * fix: migrate legacy bouncer role identities
166 lines
6.1 KiB
PHP
166 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Reporting\Http\Controllers\Company;
|
|
|
|
use App\Domains\Accounts\Models\Company;
|
|
use App\Domains\Accounts\Models\CompanySetting;
|
|
use App\Domains\Contacts\Models\Customer;
|
|
use App\Domains\Purchases\Models\Expense;
|
|
use App\Domains\Receivables\Models\Payment;
|
|
use App\Domains\Sales\Models\Estimate;
|
|
use App\Domains\Sales\Models\Invoice;
|
|
use App\Platform\Http\Controller;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Silber\Bouncer\BouncerFacade;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
/**
|
|
* Handle the incoming request.
|
|
*
|
|
* @return 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) {
|
|
$invoice_totals[] = Invoice::whereBetween(
|
|
'invoice_date',
|
|
[$start->format('Y-m-d'), $end->format('Y-m-d')]
|
|
)
|
|
->whereCompany()
|
|
->sum('base_total');
|
|
$expense_totals[] = Expense::whereBetween(
|
|
'expense_date',
|
|
[$start->format('Y-m-d'), $end->format('Y-m-d')]
|
|
)
|
|
->whereCompany()
|
|
->sum('base_amount');
|
|
$receipt_totals[] = Payment::whereBetween(
|
|
'payment_date',
|
|
[$start->format('Y-m-d'), $end->format('Y-m-d')]
|
|
)
|
|
->whereCompany()
|
|
->sum('base_amount');
|
|
$net_income_totals[] = ($receipt_totals[$i] - $expense_totals[$i]);
|
|
$i++;
|
|
$months[] = $start->translatedFormat('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();
|
|
// "How many invoices did we issue" counts issued documents, so the
|
|
// reversals are excluded. The sums above deliberately keep them: a
|
|
// credit note's negated total is exactly what nets sales back out.
|
|
$total_invoice_count = Invoice::whereCompany()
|
|
->where('type', Invoice::TYPE_INVOICE)
|
|
->count();
|
|
$total_estimate_count = Estimate::whereCompany()->count();
|
|
$total_amount_due = Invoice::whereCompany()
|
|
->sum('base_due_amount');
|
|
|
|
// Raw models, not InvoiceResource: every loaded relation is serialized
|
|
// with the full $appends set, so a column-limited creditNotes load blew
|
|
// up in the date accessors (no company_id on the children) and a full
|
|
// load would run the appends per credit note for nothing. The rows do
|
|
// not need the relation: credited_status is a resource-level field, and
|
|
// a fully credited invoice has no due amount so it never appears here.
|
|
$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,
|
|
]);
|
|
}
|
|
}
|