File "PaymentByCounterpartyResource.php"
Full Path: /var/www/html/back/app/Http/Resources/CashFlow/OtherData/PaymentByCounterpartyResource.php
File size: 3.65 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace app\Http\Resources\CashFlow\OtherData;
use App\Http\Resources\Traits\HasPaymentType;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Log;
class PaymentByCounterpartyResource extends JsonResource
{
use HasPaymentType;
protected string $paymentType;
public function __construct($resource, string $paymentType)
{
parent::__construct($resource);
$this->paymentType = $paymentType;
}
public function toArray(Request $request): array
{
$paymentType = $this->paymentType;
$totalAmount = 0;
foreach ($this->payments as $payment) {
foreach ($payment->distributions as $distribution) {
if ($paymentType == 'cash' && $distribution->cashbox != null) {
$totalAmount += $distribution->cashbox;
} elseif ($paymentType == 'commission' && $distribution->comission != null) {
$totalAmount += $distribution->comission;
} elseif ($paymentType == 'moving') {
$totalAmount += $distribution->amount ?? $distribution->cashbox;
} elseif ($paymentType == 'cash-credit' && $distribution->cashbox != null) {
$totalAmount += $distribution->cashbox;
} else {
$totalAmount += $distribution->amount;
}
}
}
return [
'id' => $this->id,
'name' => $this->name,
'inn' => $this->inn,
'kpp' => $this->kpp,
'comment' => $this->comment,
'archived' => $this->archived,
'c_type' => $this->c_type,
'total_amount' => $totalAmount,
'payments' => $this->payments->map(function ($payment) use ($paymentType) {
$paymentAmount = $payment->amount;
$firstDistribution = $payment->distributions->first();
if ($firstDistribution) {
if ($paymentType == 'cash' && $firstDistribution->cashbox != null) {
$paymentAmount = $firstDistribution->cashbox;
} elseif ($paymentType == 'commission' && $firstDistribution->comission != null) {
$paymentAmount = $firstDistribution->comission;
} elseif ($paymentType == 'moving') {
$paymentAmount = $firstDistribution->amount ?? $firstDistribution->cashbox;
} elseif ($paymentType == 'cash-credit' && $firstDistribution->cashbox != null) {
$paymentAmount = $firstDistribution->cashbox;
} else {
$paymentAmount = $firstDistribution->amount;
}
}
return [
'id' => $payment->id,
'name' => $payment->name,
'amount' => $paymentAmount,
'status' => $payment->status,
'payment_type' => $payment->payment_type,
'payment_date' => $payment->payment_date,
'project' => $firstDistribution
? [
'offer_number' => $firstDistribution->project->offer_number ?? null,
'object_address' => $firstDistribution->project->object_address ?? null,
'short_description' => $firstDistribution->project->short_description ?? null,
'description' => $firstDistribution->project->description ?? null,
] : null,
];
}),
];
}
}