File "ShowProjectGroupResource.php"

Full Path: /var/www/html/back/app/Http/Resources/CashFlow/ShowProjectGroupResource.php
File size: 6.81 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Http\Resources;

use App\Domain\Payment\Enums\PaymentTypeEnum;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ShowProjectGroupResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $totalLimitCredit = 0;
        $totalLimitDebit = 0;

        $creditAmount = 0;
        $debitAmount = 0;


        $this->projects->each(function ($item) use (&$totalLimitCredit, &$totalLimitDebit, &$creditAmount, &$debitAmount) {
            $totalLimitCredit += $item->project_limits_credit;
            $totalLimitDebit += $item->project_limits_debit;

            if (isset($item->paymentDistributions)) {
                foreach ($item->paymentDistributions as $distribution) {
                    if (($distribution->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_RECEPTION->value) ||
                        ($distribution->payment->payment_type == PaymentTypeEnum::PAYMENT_TYPE_RECEPTION_FROM_1C->value)) {
                        $creditAmount += (float)$distribution->amount;
                    } else {
                        $debitAmount += (float)$distribution->amount;
                    }
                }
            }
        });

        return [
            'id' => $this->id,
            'name' => $this->name,
            'description' => $this->description,
            'total_limits' => [
                'credit' => $totalLimitDebit,
                'debit' => $totalLimitCredit,
            ],

            'project_amounts' => [
                'credit' => $creditAmount,
                'debit' => $debitAmount,
            ],

            'payments_by_date' => $this->projects->map(function ($project) {
                return $project->paymentDistributions->map(function ($paymentDistribution) use ($project) {
                    return [
                        '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' => [
                            '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,
                        ],
                        'project' => [
                            'description' => $project->description,
                            'short_description' => $project->short_description,
                        ]
                    ];
                });
            }),


            'payments_by_article' => $this->projects->map(function ($project) {
                return [
                    'id' => $project->id,
                    'description' => $project->description,
                    'short_description' => $project->short_description,
                    'offer_number' => $project->offer_number,
                    'object_address' => $project->object_address,
                    'project_limits' => $project->project_limits,
                    'project_limits_credit' => $project->project_limits_credit,
                    'project_limits_debit' => $project->project_limits_debit,
                    'articles' => $project->paymentDistributions->map(function ($paymentDistribution) {
                        return [
                            'id' => $paymentDistribution->article->id ?? null,
                            'name' => $paymentDistribution->article->name ?? null,
                            'payment_limits' => $paymentDistribution->article->payment_limits ?? null,
                            'article_type' => $paymentDistribution->article->article_type ?? 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,
                            ],
                        ];
                    }),
                ];
            }),

            '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,
                        ];
                    }),
                ];
            }),
        ];
    }
}