Yohohohohohooho | Sanrei Aya
Sanrei Aya


Server : LiteSpeed
System : Linux barito.iixcp.rumahweb.net 5.14.0-611.49.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 21 16:39:08 EDT 2026 x86_64
User : elvh3918 ( 1528)
PHP Version : 8.2.31
Disable Function : mail
Directory :  /home/elvh3918/public_html/pmm/app/Http/Controllers/API/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/elvh3918/public_html/pmm/app/Http/Controllers/API/GlobalAPIController.php
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use App\Models\Bussiness;
use App\Models\Master\Tenant;
use App\Models\Master\Technician;
use App\Models\Master\Customer;
use App\Models\Master\Item;
use App\Models\Master\Supplier;
use App\Models\Master\Part;
use App\Models\Master\Setting\ServiceType;
use App\Models\Admin\Setting\FormatBarcode;

class GlobalAPIController extends Controller
{
    public function get_auth_user() {
        return Auth::user()->load('model');
    }

    public function select_role($role)
    {
        $query = DB::select("CALL sp_permission_by_role_view('$role')");
        return response()->json($query);
    }

    public function split_upper($data) {
        $OutString = '';
        $i = 0;
        for ($index=0; $index < count($data); $index++) {
            $cChar = $data[$index]; 
            if (preg_match('/[A-Z]/', $cChar)) {
                $OutString += ' ' + $cChar;
            } else {
                $OutString += $cChar;
            }

            $i += 1;
        }
        
        return strtolower($OutString);
    }

    public function delete_image_buss($id)
    {
        $msg = '';
        try{
            $buss = Bussiness::where('id', '=', $id)->first();
            if (Storage::disk('public')->exists(config('app.dir_file_buss') . '/'. $buss->image)) {
                Storage::disk('public')->delete(config('app.dir_file_buss') . '/' . $buss->image);
    
                $msg = 'success';
            } else {
                $msg = 'not exists';
            }

            if ($msg == 'success') {
                $buss->update([
                    'image' => null,
                ]);
            }
        } catch (\Exception $e) {
            $msg = 'failed';
        }

        return response()->json($msg);
    }

    public function delete_image_tenant($id)
    {
        $msg = '';
        try{
            $tenant = Tenant::where('id', '=', $id)->first();
            if (Storage::disk('public')->exists(config('app.dir_file_tenant') . '/'. $tenant->image)) {
                Storage::disk('public')->delete(config('app.dir_file_tenant') . '/' . $tenant->image);
    
                $msg = 'success';
            } else {
                $msg = 'not exists';
            }

            if ($msg == 'success') {
                $tenant->update([
                    'image' => null,
                ]);
            }
        } catch (\Exception $e) {
            $msg = 'failed';
        }

        return response()->json($msg);
    }

    public function delete_image_technician($id)
    {
        $msg = '';
        try{
            $technician = Technician::where('id', '=', $id)->first();
            if (Storage::disk('public')->exists(config('app.dir_file_technician') . '/'. $technician->image)) {
                Storage::disk('public')->delete(config('app.dir_file_technician') . '/' . $technician->image);
    
                $msg = 'success';
            } else {
                $msg = 'not exists';
            }

            if ($msg == 'success') {
                $technician->update([
                    'image' => null,
                ]);
            }
        } catch (\Exception $e) {
            $msg = 'failed';
        }

        return response()->json($msg);
    }

    public function delete_image_customer($id)
    {
        $msg = '';
        try{
            $customer = Customer::where('id', '=', $id)->first();
            if (Storage::disk('public')->exists(config('app.dir_file_customer') . '/'. $customer->image)) {
                Storage::disk('public')->delete(config('app.dir_file_customer') . '/' . $customer->image);
    
                $msg = 'success';
            } else {
                $msg = 'not exists';
            }

            if ($msg == 'success') {
                $customer->update([
                    'image' => null,
                ]);
            }
        } catch (\Exception $e) {
            $msg = 'failed';
        }

        return response()->json($msg);
    }

    public function delete_image_item($id)
    {
        $msg = '';
        try{
            $item = Item::where('id', '=', $id)->first();
            if (Storage::disk('public')->exists(config('app.dir_file_item') . '/'. $item->image)) {
                Storage::disk('public')->delete(config('app.dir_file_item') . '/' . $item->image);
    
                $msg = 'success';
            } else {
                $msg = 'not exists';
            }

            if ($msg == 'success') {
                $item->update([
                    'image' => null,
                ]);
            }
        } catch (\Exception $e) {
            $msg = 'failed';
        }

        return response()->json($msg);
    }

    public function select_tenant_open_customer($tenant) {
        if ($tenant == 0) {
            $query = null;
        } else {
            $query = Customer::with('tenant')->where('tenant_id', '=', $tenant)->get();
        }

        return response()->json($query);
    }

    public function delete_image_part($id)
    {
        $msg = '';
        try{
            $part = Part::where('id', '=', $id)->first();
            if (Storage::disk('public')->exists(config('app.dir_file_part') . '/'. $part->image)) {
                Storage::disk('public')->delete(config('app.dir_file_part') . '/' . $part->image);
    
                $msg = 'success';
            } else {
                $msg = 'not exists';
            }

            if ($msg == 'success') {
                $part->update([
                    'image' => null,
                ]);
            }
        } catch (\Exception $e) {
            $msg = 'failed';
        }

        return response()->json($msg);
    }

    public function select_tenant_open_supplier($supplier) {
        if ($supplier == 0) {
            $query = null;
        } else {
            $query = Supplier::with('tenant')->where('tenant_id', '=', $supplier)->get();
        }

        return response()->json($query);
    }

    public function open_tenant() {
        if (Auth::user()->model_type == 'App\Models\Master\Tenant') {
            $query = Tenant::where('is_active', '=', 1)->where('id', '=', Auth::user()->model_id)->orderBy('name', 'asc')->get();
        } else if (Auth::user()->model_type == 'App\Models\Bussiness') {
            $query = Tenant::where('is_active', '=', 1)->orderBy('name', 'asc')->get();
        }

        return response()->json($query);
    }

    public function open_technician_by_tenant($id) {
        $query = Technician::with('tenant')->where('is_active', '=', 1)->where('tenant_id', '=', $id)->get();
        return response()->json($query);
    }

    public function open_customer_by_tenant($id) {
        $query = Customer::with('tenant')->where('is_active', '=', 1);
        if ($id != 0) {
            $query = $query->where('tenant_id', '=', $id);
        }

        $query = $query->get();
        return response()->json($query);
    }

    public function open_item_by_customer($id) {
        $query = Item::with('customer.tenant', 'maintenance.technician', 'maintenance.maintenance_detail.part')->where('is_active', '=', 1)->where('customer_id', '=', $id)->get();
        return response()->json($query);
    }

    public function open_history_by_item($id) {
        $query = Item::with('customer.tenant', 'maintenance.technician', 'maintenance.maintenance_detail.part')->where('id', '=', $id)->first();
        return response()->json($query);
    }

    public function open_service_type_by_tenant($id) {
        $query = ServiceType::where('is_active', '=', 1)->where('tenant_id', '=', $id)->orderBy('seq', 'asc')->get();
        return response()->json($query);
    }

    public function open_part_by_tenant($id) {
        $query = Part::with('supplier')->where('is_active', '=', 1)->whereRelation('supplier', 'tenant_id', '=', $id)->orderBy('name', 'asc')->get();
        return response()->json($query);
    }

    public function open_supplier_by_tenant($id) {
        $query = Supplier::where('is_active', '=', 1)->where('tenant_id', '=', $id)->orderBy('name', 'asc')->get();
        return response()->json($query);
    }

    public function generate_barcode($tenant_id) {
        $tenant = Tenant::where('id', '=', $tenant_id)->first();
        $formatBarcode = FormatBarcode::where('tenant_id', '=', $tenant_id)->first();
        if ($formatBarcode) {
            $lastItem = Item::with('customer')->whereRelation('customer', 'tenant_id', '=', $tenant_id)->orderBy('id', 'desc')->count();
            $newNumber = $lastItem + 1;

            $paddedNumber = str_pad($newNumber, $formatBarcode->length, '0', STR_PAD_LEFT);
            $newBarcode = $tenant->code . '-' . $paddedNumber;

            return response()->json($newBarcode);
        } else {
            return response()->json(null);
        }
    }
}

Yohohohohohooho | Sanrei Aya