File "CashFlowCreditResource.php"

Full Path: /var/www/html/back/app/Http/Resources/Permissions/CashFlowCreditResource.php
File size: 1.29 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 CashFlowCreditResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $totalCredit = 0;
        $totalCash = 0;

        foreach ($this->paymentDistributions as $distribution) {

            $amount = (float)$distribution->amount;

            if (isset($distribution->article->article_type)) {
                if ($distribution->article->article_type === 'credit') {
                    if (isset($distribution->article_id) && $distribution->article_id == 1) {
                        $totalCash += $amount;
                    } else {
                    $totalCredit += $amount;
                    }
                }
            }
        }

        return [
            'id' => $this->id,
            'name' => $this->name,
            'default_in_project' => $this->default_in_project,
            'model_id' => $this->model_id,
            'total_amount' => $totalCredit,
            'total_cash' => $totalCash,
            'sort' => $this->sort,
            'group' => [
                'id' => $this->group->id,
                'name' => $this->group->name,
                'model_id' => $this->group->model_id,
            ]
        ];
    }
}