Add missing salesStatistics API function

This commit is contained in:
2025-11-28 09:06:34 +01:00
parent 08737ba3d1
commit 25b255a93c
+62 -4
View File
@@ -48,9 +48,8 @@ public function summaryAll()
public function summaryThisYear() public function summaryThisYear()
{ {
$dt = new DateTime('first day of january this year');
$invoices = Invoice::select() $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') ->orderBy('invoice_date', 'asc')
->orderByRaw("CAST(SUBSTRING(nr, 4) AS UNSIGNED) ASC") ->orderByRaw("CAST(SUBSTRING(nr, 4) AS UNSIGNED) ASC")
->get(); ->get();
@@ -62,9 +61,8 @@ public function summaryThisYear()
public function summaryBeforeThisYear() public function summaryBeforeThisYear()
{ {
$dt = new DateTime('first day of january this year');
$invoices = Invoice::select() $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') ->orderBy('invoice_date', 'asc')
->orderByRaw("CAST(SUBSTRING(nr, 4) AS UNSIGNED) ASC") ->orderByRaw("CAST(SUBSTRING(nr, 4) AS UNSIGNED) ASC")
->get(); ->get();
@@ -116,6 +114,66 @@ public static function single($id)
return ApiDataTransformer::snakeToCamel($invoiceArray); 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) public function preview($id)
{ {