FabioFratini hai 8 meses
pai
achega
93c789c3ea
Modificáronse 1 ficheiros con 33 adicións e 25 borrados
  1. 33 25
      routes/web.php

+ 33 - 25
routes/web.php

@@ -1466,11 +1466,9 @@ Route::get('/get_course_members', function () {
 });
 
 Route::get('/get_receipts', function () {
-    // Start with a base query - but without the GROUP BY for counting
-    $baseQuery = \App\Models\Receipt::select('receipts.*')
+    $baseQuery = \App\Models\Receipt::select('receipts.id')
         ->leftJoin('members', 'receipts.member_id', '=', 'members.id');
 
-    // Apply filters
     if (isset($_GET["search"]["value"]) && !empty($_GET["search"]["value"])) {
         $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
         $member_ids = \App\Models\Member::where(function ($query) use ($v) {
@@ -1492,21 +1490,35 @@ Route::get('/get_receipts', function () {
     if (isset($_GET["filterMember"]) && $_GET["filterMember"] != "")
         $baseQuery = $baseQuery->where('receipts.member_id', $_GET["filterMember"]);
 
-    // Count BEFORE applying the GROUP BY
-    $count = $baseQuery->distinct()->count('receipts.id');
-
-    // Clone the base query and add the group by and joins for the data
-    $dataQuery = clone $baseQuery;
-    $dataQuery = $dataQuery->select(
-        'receipts.*',
-        'members.first_name',
-        'members.last_name',
-        DB::raw('SUM(receipts_rows.amount) AS totals')
-    )
-    ->leftJoin('receipts_rows', 'receipts.id', '=', 'receipts_rows.receip_id')
-    ->groupBy('receipts.id');
-
-    // Apply sorting
+    $count = $baseQuery->count();
+
+    $receiptIds = $baseQuery->pluck('receipts.id');
+
+    $dataQuery = \App\Models\Receipt::select(
+            'receipts.id',
+            'receipts.year',
+            'receipts.number',
+            'receipts.status',
+            'receipts.created_at',
+            'receipts.record_id',
+            'members.first_name',
+            'members.last_name',
+            DB::raw('SUM(receipts_rows.amount) AS totals')
+        )
+        ->leftJoin('members', 'receipts.member_id', '=', 'members.id')
+        ->leftJoin('receipts_rows', 'receipts.id', '=', 'receipts_rows.receip_id')
+        ->whereIn('receipts.id', $receiptIds)
+        ->groupBy(
+            'receipts.id',
+            'receipts.year',
+            'receipts.number',
+            'receipts.status',
+            'receipts.created_at',
+            'receipts.record_id',
+            'members.first_name',
+            'members.last_name'
+        );
+
     if (isset($_GET["order"])) {
         $column = '';
         if ($_GET["order"][0]["column"] == 0)
@@ -1520,7 +1532,7 @@ Route::get('/get_receipts', function () {
         if ($_GET["order"][0]["column"] == 4)
             $column = 'receipts.status';
         if ($_GET["order"][0]["column"] == 5)
-            $column = 'receipts.date';
+            $column = 'receipts.created_at';
         if ($_GET["order"][0]["column"] == 6)
             $column = 'totals';
 
@@ -1532,22 +1544,19 @@ Route::get('/get_receipts', function () {
         $dataQuery = $dataQuery->orderBy('receipts.id', 'DESC');
     }
 
-    // Apply pagination
     if (isset($_GET["start"]) && isset($_GET["length"]))
         $dataQuery = $dataQuery->offset($_GET["start"])->limit($_GET["length"]);
 
-    // Get the results
     $results = $dataQuery->get();
 
-    // Format the data
     $datas = [];
     foreach ($results as $idx => $r) {
         $ids = $r->id . "|" . $r->record_id;
         $datas[] = array(
             'year' => $r->year,
             'number' => $r->number,
-            'last_name' => $r->member->last_name,
-            'first_name' => $r->member->first_name,
+            'last_name' => $r->last_name ?? '',
+            'first_name' => $r->first_name ?? '',
             'status' => $r->status,
             'date' => date("d/m/Y", strtotime($r->created_at)),
             'totals' => formatPrice($r->totals),
@@ -1555,7 +1564,6 @@ Route::get('/get_receipts', function () {
         );
     }
 
-    // Return the proper format with correct counts
     return response()->json([
         "data" => $datas,
         "recordsTotal" => $count,