Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/medad.corals.io/Corals/modules/Medad/Classes/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/medad.corals.io/Corals/modules/Medad/Classes/Medad.php
<?php

namespace Corals\Modules\Medad\Classes;

use Corals\Modules\Medad\Constants\CompanyTypeConstants;
use Corals\Modules\Medad\Models\Branch;
use Corals\Modules\Medad\Models\Company;
use Corals\Modules\Medad\Models\CompanyRelation;
use Corals\Modules\Medad\Models\CompanyRelationBranch;
use Corals\Modules\Medad\Models\Project;
use Corals\User\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class Medad
{
    /**
     * Medad constructor.
     */
    function __construct()
    {
    }

    public function getUserCompany($user = null)
    {
        if (is_null($user)) {
            $user = \user();
        }

        return cache()->remember('userCompany_' . $user->id, 720, function () use ($user) {
            $userCompany = DB::table('medad_company_user')->where('user_id', $user->id)->first();

            if (!$userCompany) {
                return null;
            }

            return Company::find($userCompany->company_id);
        });
    }

    public function getCurrentCompanyType($company = null)
    {
        if (is_api_request()) {
            $token = user()->token();
            if (is_array($token->properties)) {
                $properties = $token->properties;
            } else {
                $properties = json_decode($token->properties ?? '', true);
            }

            $type = data_get($properties, 'acting_company_type');
        } else {
            $type = session()->get('acting_company_type');
        }

        if (!$type && $company) {
            $type = $company->type;
        }

        return $type;
    }

    public function isCompanyType($company, $targetType, $bothIncluded = true)
    {
        $companyTypeArray = [$targetType];

        if ($bothIncluded) {
            $companyTypeArray[] = CompanyTypeConstants::BOTH;
        }

        return in_array($this->getCurrentCompanyType($company), $companyTypeArray);
    }

    public function getCompanyOppositeType($companyType = null)
    {
        if (is_null($companyType)) {
            $companyType = $this->getCurrentCompanyType();
        }

        return $companyType == CompanyTypeConstants::CUSTOMER ? CompanyTypeConstants::SUPPLIER : CompanyTypeConstants::CUSTOMER;
    }

    public function getCompanyUsers($role = null, $company = null)
    {
        $users = User::query();

        if ($role) {
            $users->whereHas('roles', function (Builder $query) use ($role) {
                $query->whereIn('name', Arr::wrap($role));
            });
        }

        if (!$company) {
            $company = $this->getUserCompany();
        }

        $users->when($company, function ($companyUserQB, $company) {
            $companyUserQB->join('medad_company_user', 'medad_company_user.user_id', '=', 'users.id')
                ->where('medad_company_user.company_id', $company->id);
        });

        return $users->select('users.*')->get()->pluck('full_name', 'id');
    }

    public function getCompanyAdmin($user = null)
    {
        if (is_null($user)) {
            $user = \user();
        }

        return Company::whereHas('admins', function ($query) use ($user) {
            $query->where('user_id', $user->id);
        })->first();
    }

    public function getCompanyBranches($company, $asObject = false)
    {
        if (!($company instanceof Company)) {
            $company = Company::findOrFail($company);
        }


        $userBranches = $this->getUserBranches($company);

        if ($asObject) {
            return $userBranches;
        }

        $branches = [];

        $userBranches->each(function ($branch) use (&$branches) {
            $branches[$branch->id] = $branch->presentStripTags('name');
        });

        return $branches;
    }

    public function getCompanySuppliers($company)
    {
        if ($company instanceof Branch) {
            return $this->getRelatedCompaniesBasedOnBranch($company, CompanyTypeConstants::SUPPLIER);
        }

        return $company->companyRelations()
            ->where('relation_type', CompanyTypeConstants::SUPPLIER)
            ->pluck('name', 'second_company_id')->toArray();
    }

    public function getCompanyCustomers($company)
    {
        if ($company instanceof Branch) {
            return $this->getRelatedCompaniesBasedOnBranch($company, CompanyTypeConstants::CUSTOMER);
        }

        return $company->companyRelations()
            ->where('relation_type', CompanyTypeConstants::CUSTOMER)
            ->pluck('name', 'second_company_id')->toArray();
    }

    public function getProjects($branch)
    {
        if ($branch instanceof Branch) {
            $branchId = $branch->id;
        } else {
            $branchId = $branch;
        }
        $projects = Project::where('branch_id', $branchId);

        if (user()->hasRole('medad_project_manager')) {
            $projects->where('manager_id', user()->id);
        }

        return $projects->pluck('name', 'id');
    }

    public function getRelatedCompaniesBasedOnBranch($branch, $companyRelationType)
    {
        $companiesRelatedToBranch = CompanyRelationBranch::query()
            ->join('medad_company_relation', 'medad_company_relation.id', '=',
                'company_relation_branches.company_relation_id')
            ->join('medad_companies', 'medad_companies.id', '=', 'medad_company_relation.second_company_id')
            ->select('medad_companies.id', 'medad_companies.name')
            ->where('medad_company_relation.relation_type', $companyRelationType)
            ->where('company_relation_branches.branch_id', $branch->id)
            ->pluck('medad_companies.name', 'medad_companies.id')
            ->toArray();


        $companiesWithoutBranchesAssigned = CompanyRelation::query()
            ->join('medad_companies', 'medad_companies.id', '=', 'medad_company_relation.second_company_id')
            ->select('medad_company_relation.second_company_id', 'medad_companies.name')
            ->where('medad_company_relation.relation_type', $companyRelationType)
            ->where('medad_company_relation.company_id', $branch->parent_id)
            ->whereDoesntHave('branches')
            ->pluck('medad_companies.name', 'second_company_id')->toArray();

        return $companiesRelatedToBranch + $companiesWithoutBranchesAssigned;
    }

    public function getCompanies($target)
    {
        $userCompany = $this->getUserCompany();

        if (!$userCompany) {
            return Company::query()
                ->where('type', $target)
                ->orWhere('type', CompanyTypeConstants::BOTH)
                ->pluck('name', 'id');
        }

        if ($target == CompanyTypeConstants::CUSTOMER) {
            return $this->getCompanyCustomers($userCompany);
        } else {
            return $this->getCompanySuppliers($userCompany);
        }
    }

    public function getUserBranches($company = null, $user = null)
    {
        if (is_null($company)) {
            $company = $this->getUserCompany();
        }
        if (is_null($user)) {
            $user = user();
        }
        if ($user->hasRole('medad_company_admin')) {
            return $company->branches;
        }

        $branches = $company->branches()->join('medad_company_user', 'medad_company_user.company_id',
            'medad_companies.id')
            ->where('medad_company_user.user_id', $user->id)
            ->select('medad_companies.*')->get();

        if ($branches->isEmpty()) {
            //then assigned to all branches
            $branches = $company->branches;
        }

        return $branches;
    }

    public function getManagerProjects($managerId)
    {
        return Project::query()->where('manager_id', $managerId)->get();
    }

    public function getCode($table,$codePrefix,$number)
    {
        do {
            $number++;

            $code = $codePrefix . '_' . sprintf('%s-%02d', $table::$codePrefix, $number);

            $recordExists = $table::query()->where('code', $code)->first();
        } while ($recordExists);

        return $code;
    }
}

Spamworldpro Mini