Procházet zdrojové kódy

Fix reclamation report spare parts totals

Alexander Musikhin před 4 dny
rodič
revize
df1b9cf70f

+ 32 - 1
app/Http/Controllers/ReportController.php

@@ -12,6 +12,7 @@ use App\Models\ProductSKU;
 use App\Models\Reclamation;
 use App\Models\ReclamationStatus;
 use App\Models\User;
+use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Http\Request;
 use Illuminate\Support\Str;
 
@@ -134,7 +135,15 @@ class ReportController extends Controller
             $this->data['reclamations'][$reclamationStatus] = Reclamation::query()->where('status_id', '=', $reclamationStatusId)->count();
         }
         $this->data['reclamationMafs'] = $this->data['reclamationDetails'] = [];
-        foreach (Reclamation::all() as $reclamation) {
+        $reclamations = Reclamation::query()
+            ->with([
+                'skus.product',
+                'details',
+                'spareParts',
+            ])
+            ->get();
+
+        foreach ($reclamations as $reclamation) {
             foreach ($reclamation->skus as $sku) {
                 $a = $sku->product->article;
                 if(isset($this->data['reclamationMafs'][$a])){
@@ -154,6 +163,8 @@ class ReportController extends Controller
             }
         }
 
+        $this->appendReclamationSparePartsToReport($reclamations);
+
 
         // svod
         $orderStatuses = OrderStatus::query()->get()->pluck('name', 'id')->toArray();
@@ -263,6 +274,26 @@ class ReportController extends Controller
         return view('reports.index', $this->data);
     }
 
+    private function appendReclamationSparePartsToReport(Collection $reclamations): void
+    {
+        foreach ($reclamations as $reclamation) {
+            foreach ($reclamation->spareParts as $sparePart) {
+                $article = trim((string) $sparePart->article);
+                $quantity = (int) ($sparePart->pivot?->quantity ?? 0);
+
+                if ($article === '' || $quantity < 1) {
+                    continue;
+                }
+
+                if (isset($this->data['reclamationDetails'][$article])) {
+                    $this->data['reclamationDetails'][$article] += $quantity;
+                } else {
+                    $this->data['reclamationDetails'][$article] = $quantity;
+                }
+            }
+        }
+    }
+
     private function getOrderCount($districtId, $status = null, $type = null)
     {
         $q = Order::query()->where('district_id', '=', $districtId);

+ 38 - 0
tests/Feature/ReportControllerTest.php

@@ -2,7 +2,11 @@
 
 namespace Tests\Feature;
 
+use App\Models\Order;
+use App\Models\Reclamation;
+use App\Models\ReclamationDetail;
 use App\Models\Role;
+use App\Models\SparePart;
 use App\Models\User;
 use Illuminate\Foundation\Testing\RefreshDatabase;
 use Tests\TestCase;
@@ -78,6 +82,40 @@ class ReportControllerTest extends TestCase
         $response->assertViewHas('reclamationStatuses');
     }
 
+    public function test_reports_index_aggregates_reclamation_details_and_spare_parts(): void
+    {
+        $order = Order::factory()->create(['user_id' => $this->adminUser->id]);
+        $reclamation = Reclamation::factory()->create([
+            'order_id' => $order->id,
+            'user_id' => $this->adminUser->id,
+        ]);
+
+        ReclamationDetail::query()->create([
+            'reclamation_id' => $reclamation->id,
+            'name' => 'Петля',
+            'quantity' => 2,
+        ]);
+
+        $sparePart = SparePart::factory()->create([
+            'article' => 'SP-REPORT-001',
+        ]);
+
+        $reclamation->spareParts()->attach($sparePart->id, [
+            'quantity' => 3,
+            'with_documents' => false,
+            'status' => 'reserved',
+            'reserved_qty' => 3,
+            'issued_qty' => 0,
+        ]);
+
+        $response = $this->actingAs($this->adminUser)->get(route('reports.index'));
+
+        $response->assertViewHas('reclamationDetails', function (array $details) {
+            return ($details['Петля'] ?? null) === 2
+                && ($details['SP-REPORT-001'] ?? null) === 3;
+        });
+    }
+
     public function test_reports_index_has_by_district_data(): void
     {
         $response = $this->actingAs($this->adminUser)->get(route('reports.index'));