File "PaymentLogService.php"

Full Path: /var/www/html/back/app/Domain/Payment/StatusModels/Reception/PaymentLogService.php
File size: 2.33 KB
MIME-type: text/x-php
Charset: utf-8

<?php

declare(strict_types=1);

namespace App\Domain\Payment;

use App\BaseClasses\BaseService;
use App\Domain\Payment\Enums\PaymentStatusEnum;
use App\Models\Payment;
use App\Models\PaymentLog;

class PaymentLogService extends BaseService
{
    public function store(int $userID, Payment $payment, $oldStatus, ?int $distributionID, string $comment, ?array $changes): void
    {
        PaymentLog::create([
            'status' => $oldStatus,
            'payment_id' => $payment->id,
            'payment_distribution_id' => $distributionID,
            'user_id' => $userID,
            'comment' => $comment,
            'changes' => $changes,
        ]);
    }

    public static function readableArrayFromRecord(PaymentLog $log): array
    {
        #return $log->toArray();
        $result = [
            'id' => $log->id,
            'comment' => $log->comment,
            'user' => $log->user,
            'created_at' => $log->created_at,
            'status' => PaymentStatusEnum::tryFrom($log->status)->value
        ];
        $changes = [];
        foreach ($log->changes ?? [] as $change => $values) {
            if (empty($values) || (is_array($values) && count($values) === 0)) {
                continue;
            }
            $title = match ($change) {
                'status' => 'Изменен статус',
                'organization' => 'Изменена организация',
                'contragent' => 'Изменен контрагент',
                'amount' => 'Изменена сумма',
                'project' => 'Изменен проект',
                'distribution' => 'Изменено распределение',
                'article' => 'Изменена статья',
                'account_id'=> 'Изменен счет',
                'payment_date'=>'Изменена плановая дата',
                default => 'Изменения',
            };
            if ($change == 'status') {
                $values = [
                    PaymentStatusEnum::tryFrom($values[0])?->title(),
                    PaymentStatusEnum::tryFrom($values[1])?->title(),
                ];
            }
            $changes[] = [
                'title' => $title,
                'values' => $values,
            ];
        }
        $result['changes'] = $changes;

        return $result;
    }
}