HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.shooperm.com/Modules/Account/Repositories/ChartOfAccountRepository.php
<?php

namespace Modules\Account\Repositories;

use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Modules\Account\Entities\ChartOfAccount;

class ChartOfAccountRepository
{
    /**
     * @var ChartOfAccount
     */
    protected $chartOfAccount;

    /**
     * ChartOfAccountRepository constructor.
     * @param ChartOfAccount $chartOfAccount
     */
    public function __construct(ChartOfAccount $chartOfAccount)
    {
        $this->chartOfAccount = $chartOfAccount;
    }

    public function pluckAll($id = null)
    {
        $query = $this->chartOfAccount->select(DB::raw('CONCAT(name, " (", code, ")") AS full_name'), 'id')->where('status', 1)->get();
        if ($id) {
            $query = $query->except($id);
        }
        return $query->pluck('full_name', 'id')->prepend(__('chart_of_account.Add As A Parent Account'), '');
    }

    public function pluckByType($type)
    {
        return $this->chartOfAccount->select(DB::raw('CONCAT(name, " (", code, ")") AS full_name'), 'id')->where('status', 1)->whereIn('type', $type)->get()
            ->pluck('full_name', 'id')->prepend(__('chart_of_account.Select Account'), '');
    }

    public function create($request)
    {
        return $this->chartOfAccount->forceCreate($this->formatRequest($request));
    }

    public function update($request, $id)
    {
        $chartOfAccount = $this->find($id);
        $chartOfAccount->forceFill($this->formatRequest($request, $chartOfAccount))->save();
        return $chartOfAccount->refresh();
    }

    /**
     * @throws \Throwable
     */
    public function find($id, array $with = [])
    {
        $chartOfAccount = $this->chartOfAccount->with($with)->find($id);
        throw_if(!$chartOfAccount, ValidationException::withMessages(['message' =>  __('chart_of_account.The requested chart of account is not found')]));

        return $chartOfAccount;
    }


    /**
     * @throws \Throwable
     */
    public function findDefaultAccount($type, array $with = [])
    {
        return $this->chartOfAccount->with($with)->where('default_for', $type)->first();
    }

    private function formatRequest($request, $chartOfAccount = null): array
    {
        $formatRequest =  [
            'name' => gv($request, 'name'),
            'code' => gv($request, 'code'),
            'opening_balance' => gv($request, 'opening_balance', 0),
            'description' => gv($request, 'description'),
            'default_for' => gv($request, 'default_for'),
            'status' => gbv($request, 'status'),
        ];

        if (!$chartOfAccount) {
            $formatRequest['parent_id'] = gv($request, 'parent_id');
            $formatRequest['type'] = gv($request, 'type');
        }

        return $formatRequest;
    }

    public function delete($id)
    {
        $chartOfAccount = $this->deleteAble($id);
        return $chartOfAccount->delete();
    }

    private function deleteAble($id)
    {
        $chartOfAccount = $this->find($id, ['childs', 'transactions']);
        throw_if($chartOfAccount->childs()->count(), ValidationException::withMessages(['message' => __("account::chart_of_account.You cann\'t delete an account which has child element")]));
        throw_if($chartOfAccount->transactions()->count(), ValidationException::withMessages(['message' => __('chart_of_account.Account has Transaction')]));

        return $chartOfAccount;
    }
}