<?php namespace app\Http\Resources\CashFlow\Project; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class ShowProjectResource extends JsonResource { public function toArray(Request $request): array { $totalCount = $this->paymentDistributions->count(); $totalAmount = $this->paymentDistributions->sum(function ($item) { return $item->amount; }); return [ 'total' => [ 'count' => $totalCount, 'amount' => $totalAmount, ], 'id' => $this->id, 'offer_number' => $this->offer_number, 'short_description' => $this->short_description, 'description' => $this->description, 'payments_by_date' => $this->paymentDistributions->map(function ($item) { return [ 'date' => $item->payment->payment_date, 'payment' => [ 'id' => $item->payment->id, 'name' => $item->payment->name, 'amount' => $item->amount, 'status' => $item->payment->status, 'payment_type' => $item->payment->payment_type, 'payment_date' => $item->payment->payment_date, 'counterparty' => $item->payment->contragent ? [ 'id' => $item->payment->contragent->id, 'name' => $item->payment->contragent->name, 'inn' => $item->payment->contragent->inn, 'kpp' => $item->payment->contragent->kpp, 'comment' => $item->payment->contragent->comment, 'c_type' => $item->payment->contragent->c_type, ] : null, ], ]; }), 'payments_by_counterparty' => $this->counterparties->map(function ($item) { $totalAmount = $item->payments->sum('amount'); return [ 'id' => $item->id, 'name' => $item->name, 'inn' => $item->inn, 'kpp' => $item->kpp, 'comment' => $item->comment, 'archived' => $item->archived, 'c_type' => $item->c_type, 'total_amount' => $totalAmount, 'payments' => $item->payments->map(function ($payment) { return [ 'id' => $payment->id, 'name' => $payment->name, 'amount' => $payment->amount, 'status' => $payment->status, 'payment_type' => $payment->payment_type, 'payment_date' => $payment->payment_date, ]; }), ]; }), ]; } }