File "ShowProjectResource.php"
Full Path: /var/www/html/back/app/Http/Resources/User/ShowProjectResource.php
File size: 5.53 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ShowProjectResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray($request)
{
$uniqueArticles = $this->articleProjects
->groupBy('article.id')
->map(function ($group) {
$totalAmount = $group->sum(function ($paymentDistribution) {
return $paymentDistribution->amount;
});
$paymentDistribution = $group->first();
return [
'id' => $paymentDistribution->article->id ?? null,
'name' => $paymentDistribution->article->name ?? null,
'default_in_project' => $paymentDistribution->article->default_in_project ?? false,
'amount_limit' => $paymentDistribution->article?->articleProjectLinks
->where('project_id', $paymentDistribution->project_id)
->first()->amount_limit ?? 0.00 ,
'article_type' => $paymentDistribution->article->article_type ?? null,
'payment' => [
'id' => $paymentDistribution->payment->id,
'name' => $paymentDistribution->payment->name,
'amount' => $totalAmount,
'status' => $paymentDistribution->payment->status,
'payment_type' => $paymentDistribution->payment->payment_type,
'payment_date' => $paymentDistribution->payment->payment_date,
] ?? $paymentDistribution->payment ?? null,
];
});
$uniqueArticlesProject = $this->articleProjectLinks->map(function ($item) {
return [
'id' => $item->article->id,
'name' => $item->article->name,
'default_in_project' => $item->article->default_in_project,
'article_type' => $item->article->article_type,
'amount_limit' => $item->amount_limit,
];
})->unique('id');
$combinedUniqueArticles = $uniqueArticles->concat($uniqueArticlesProject)->unique('id');
return [
'id' => $this->id,
'description' => $this->description,
'short_description' => $this->short_description,
'offer_number' => $this->offer_number,
'object_address' => $this->object_address,
'project_limits' => $this->project_limits,
'project_limits_credit' => $this->project_limits_credit,
'project_limits_debit' => $this->project_limits_debit,
'status' => $this->status,
'project_group' => $this->projectGroup ? [
'id' => $this->projectGroup->id,
'name' => $this->projectGroup->name,
] : null,
'payments_by_date' => $this->paymentDistributions->map(function ($item) {
return [
'id' => $item->id,
'amount' => $item->amount,
'comission' => $item->comission,
'cashbox' => $item->cashbox,
'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_article' => $combinedUniqueArticles,
'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' => number_format($totalAmount, 2, '.', ''),
'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,
];
}),
];
}),
];
}
}