File "CashFlowShowArticlesGroup.php"
Full Path: /var/www/html/back/app/Http/Resources/CashFlowShowArticlesGroup.php
File size: 4.6 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class CashFlowShowArticlesGroup extends JsonResource
{
public function toArray(Request $request): array
{
$totalCount = 0;
$totalAmount = 0;
$this->articles->each(function ($item) use (&$totalCount, &$totalAmount) {
if ($item->paymentDistributions) {
$totalCount += $item->paymentDistributions->count();
$totalAmount += $item->paymentDistributions->sum(function ($item) {
return $item ? $item->amount : 0;
});
}
});
return [
'total' => [
'count' => $totalCount,
'amount' => $totalAmount,
],
'id' => $this->id,
'name' => $this->name,
'payments_by_date' => $this->articles->flatMap(function ($article) {
return $article->paymentDistributions->map(function ($paymentDistribution) {
return [
'date' => $paymentDistribution->payment ? $paymentDistribution->payment->payment_date : null,
'project' => $paymentDistribution->project ? [
'id' => $paymentDistribution->project->id,
'description' => $paymentDistribution->project->description,
'short_description' => $paymentDistribution->project->short_description,
] : null,
'payment' => [
'id' => $paymentDistribution->payment->id,
'name' => $paymentDistribution->payment->name,
'amount' => $paymentDistribution->amount,
'status' => $paymentDistribution->payment->status,
'payment_type' => $paymentDistribution->payment->payment_type,
'payment_date' => $paymentDistribution->payment->payment_date,
'counterparty' => $paymentDistribution->payment->contragent ? [
'id' => $paymentDistribution->payment->contragent->id,
'name' => $paymentDistribution->payment->contragent->name,
'inn' => $paymentDistribution->payment->contragent->inn,
'kpp' => $paymentDistribution->payment->contragent->kpp,
'comment' => $paymentDistribution->payment->contragent->comment,
'c_type' => $paymentDistribution->payment->contragent->c_type,
] : null,
],
];
});
}),
'payments_by_counterparty' => $this->counterparties->map(function ($item) {
$totalAmount = $item->payments->flatMap(function ($payment) {
return $payment->distributions;
})->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->distributions->sum('amount'),
'status' => $payment->status,
'payment_type' => $payment->payment_type,
'payment_date' => $payment->payment_date,
'project' => $payment->distributions->first()
? [
'offer_number' => $payment->distributions->first()->project->offer_number ?? null,
'object_address' => $payment->distributions->first()->project->object_address ?? null,
'short_description' => $payment->distributions->first()->project->short_description ?? null,
'description' => $payment->distributions->first()->project->description ?? null,
] : null,
];
}),
];
}),];
}
}