diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index ecaf4f7..5e5c986 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -48,9 +48,8 @@ public function summaryAll() public function summaryThisYear() { - $dt = new DateTime('first day of january this year'); $invoices = Invoice::select() - ->where('invoice_date', '>=', $dt->format('Y-m-d')) + ->where('invoice_date', '>=', (new DateTime('first day of january this year')->format('Y-m-d'))) ->orderBy('invoice_date', 'asc') ->orderByRaw("CAST(SUBSTRING(nr, 4) AS UNSIGNED) ASC") ->get(); @@ -62,9 +61,8 @@ public function summaryThisYear() public function summaryBeforeThisYear() { - $dt = new DateTime('first day of january this year'); $invoices = Invoice::select() - ->where('invoice_date', '<', $dt->format('Y-m-d')) + ->where('invoice_date', '<', (new DateTime('first day of january this year')->format('Y-m-d'))) ->orderBy('invoice_date', 'asc') ->orderByRaw("CAST(SUBSTRING(nr, 4) AS UNSIGNED) ASC") ->get(); @@ -116,6 +114,66 @@ public static function single($id) return ApiDataTransformer::snakeToCamel($invoiceArray); } + /** + * Get yearly statistics for invoices + */ + public function salesStatistics() + { + $currentYear = date('Y'); + + // Basis-Abfrage für das aktuelle Jahr + $invoicesQuery = Invoice::whereYear('invoice_date', $currentYear); + + // Gesamtumsatz (alle außer cancelled) + $totalRevenue = $invoicesQuery->clone() + ->where('payment_status', '!=', 'cancelled') + ->sum('total_amount'); + + // Bezahlt (paid) + $paid = $invoicesQuery->clone() + ->where('payment_status', 'paid') + ->sum('total_amount'); + + // Noch nicht gestellt (draft) + $draft = $invoicesQuery->clone() + ->where('payment_status', 'draft') + ->sum('total_amount'); + + // Offene Rechnungen (issued) + $issued = $invoicesQuery->clone() + ->where('payment_status', 'issued') + ->sum('total_amount'); + + // Fällige Rechnungen (due) + $due = $invoicesQuery->clone() + ->where('payment_status', 'due') + ->sum('total_amount'); + + // Gemahnte Rechnungen (reminded) + $reminded = $invoicesQuery->clone() + ->where('payment_status', 'reminded') + ->sum('total_amount'); + + // Statistikdaten zusammenstellen + $statistics = [ + 'year' => $currentYear, + 'totalRevenue' => $totalRevenue, + 'paid' => $paid, + 'paidPercent' => round(($paid / $totalRevenue) * 100, 2), + 'draft' => $draft, + 'draftPercent' => round(($draft / $totalRevenue) * 100, 2), + 'issued' => $issued, + 'issuedPercent' => round(($issued / $totalRevenue) * 100, 2), + 'due' => $due, + 'duePercent' => round(($due / $totalRevenue) * 100, 2), + 'reminded' => $reminded, + 'remindedPercent' => round(($reminded / $totalRevenue) * 100, 2), + ]; + + // Daten in camelCase umwandeln + return ApiDataTransformer::snakeToCamel($statistics); + } + public function preview($id) {