HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: 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_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/ipsremont-demo/app/Services/Part/PartService.php
<?php


namespace App\Services\Part;


use App\Http\Requests\Part\DeleteRequest;
use App\Http\Requests\Part\IndexPartRequest;
use App\Http\Requests\Part\SchemaPartsRequest;
use App\Models\Part;
use App\Models\PartsStorage;
use App\Models\SchemaParts;
use App\Repository\Part\PartRepository;
use App\Services\Branch\BranchService;
use App\Services\ImportService;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use stdClass;

class PartService
{

    /** @var PartRepository */
    protected static PartRepository $repository;

    public static function setRepository()
    {
        self::$repository = new PartRepository();
    }

    /**
     * @return PartRepository
     */
    public static function getRepository()
    {
        self::setRepository();

        return self::$repository;
    }

    /**
     *  constructor.
     */
    public function __construct()
    {
        self::setRepository();
    }

    /**
     * @return array
     */
    public static function getAvailable()
    {
        return self::getRepository()::getAvailable()->get()->pluck('name', 'id')->toArray();
    }

    /**
     * @param $id
     *
     * @return mixed
     */
    public static function getById($id)
    {
        return self::getRepository()::getAvailable()->findOrFail($id);
    }


    /**
     * @param IndexPartRequest $request
     *
     * @return mixed
     */
    public function search(IndexPartRequest $request)
    {
        return self::getRepository()->search($request);
    }

    /**
     * Return parts amount per warehouse
     *
     * @param Part $part
     * @param User $user
     *
     * @return stdClass {local, branch, central}
     */
    public static function getStorageInfo(Part $part, User $user = null)
    {
        if (!$user) {
            $user = Auth::user();
        }
        $central = BranchService::getCentral();

        $amount = new stdClass();
        $amount->local = 0;
        $amount->branch = 0;
        $amount->central = 0;

        if ($user->getServiceWarehousePartsId()) {
            $localAmount = PartsStorage::my()
                ->where('part_id', $part->id)
                ->where('external_warehouse_id', $user->getServiceWarehousePartsId())
                ->where('type', PartsStorage::service)
                ->first('amount');
            $amount->local = $localAmount->amount ?? 0;
        }

        if ($user->getBranchWarehousePartsId()) {
            $branchAmount = PartsStorage::my()
                ->where('part_id', $part->id)
                ->whereIn('external_warehouse_id', $user->getBranchWarehousePartsId())
                ->where('type', PartsStorage::branch)
                ->pluck('amount', 'external_warehouse_id');
            $amount->branch = $branchAmount[0]->amount ?? 0;
            foreach ($user->getBranchWarehousePartsId() as $value) {
                $amount->$value = $branchAmount[$value] ?? 0;
            }
        }

        $centralAmount = PartsStorage::my()
            ->where('part_id', $part->id)
            ->where('external_warehouse_id', $central->external_id)
            ->where('type', PartsStorage::branch)
            ->first('amount');
        $amount->central = $centralAmount->amount ?? 0;

        return $amount;
    }

    public static function searchByNameOrCode($search)
    {
        return PartRepository::searchByNameOrCode($search);
    }

    public static function searchByCode($code)
    {
        return PartRepository::searchByCode($code);
    }

    public static function saveFromImport($data)
    {
        $code = $data->code;
        $part = [
            'name' => $data->name,
            'item' => $data->unit,
            'price' => $data->price,
            'foto' => $data->foto,
        ];
        self::save($code, $part);
    }

    public static function save($code, $data)
    {
        PartRepository::save($code, $data);
    }

    public static function fillStorage($part, $warehouseId, $amount)
    {
        PartRepository::fillStorage($part, $warehouseId, $amount);
    }

    protected static function formattingData($accessory, $schema_external_id): array
    {
        $data = [
            'label' => (string) $accessory->number ?? '',
            'amount' => (int) $accessory->quantity,
            'part_external_id' => (string) $accessory->code,
            'schema_external_id' => (string) $schema_external_id,
            'action' => (string) $accessory->action ?? '',
        ];

        array_walk($data, function (&$value) {
            $value = trim($value);
        });

        return $data;
    }

    public static function import($accessory, $schema_external_id): array
    {
        $data = self::formattingData($accessory, $schema_external_id);
        $result = 0;

        switch ($data['action']) {
            case '':
            case ImportService::NEW:
            case ImportService::UPDATE:
                $request = new SchemaPartsRequest();
                $validator = Validator::make($data, $request->rules());

                if ($validator->fails()) {
                    return ['result' => $validator->errors()];
                }

                $sp = SchemaParts::query()->where('part_external_id', $data['part_external_id'])->where('schema_external_id', $data['schema_external_id'])->first();
                if (!$sp) {
                    $sp = new SchemaParts();
                    $sp->fill($data);
                    $sp->save();
                } else {
                    $sp->fill($data);
                    $sp->update();
                }
                $result = 1;
                break;
            case ImportService::DELETE:
                $request = new DeleteRequest();
                $validator = Validator::make($data, $request->rules());

                if ($validator->fails()) {
                    return ['result' => $validator->errors()];
                }
                SchemaParts::query()->where('part_external_id', $data['part_external_id'])->where('schema_external_id', $data['schema_external_id'])->delete();
                $result = 1;
                break;
        }

        return ['result' => $result];
    }
}