Untitled

mail@pastecode.io avatar
unknown
php
2 months ago
11 kB
9
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Company;
use App\Models\Customer;
use App\Models\Employee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Intervention\Image\Facades\Image;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\Validator;

class CompanyController extends Controller
{
    public function getCompanies(Request $request)
    {
        $data = Company::latest()->get();

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($row) {
                $actionBtn = '<a href="'. url('company/show', $row->id) .'" class="edit btn btn-success btn-sm">Ver</a>';
                return $actionBtn;
            })
            ->rawColumns(['action'])
            ->make(true);
    }

    public function getCompanyEmployees(Request $request, $id)
    {
        $data = Employee::where('company_id', $id)->get();

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('name', function ($row) {
                return $row->user->name;
            })
            ->addColumn('email', function ($row) {
                return $row->user->email;
            })
            ->addColumn('status', function ($row) {
                return $row->user->is_active;
            })
            ->addColumn('action', function ($row) {
                $actionBtn = '<a href="'. url('employee/show', $row->user->id) .'" class="edit btn btn-success btn-sm">Ver</a>';
                return $actionBtn;
            })
            ->rawColumns(['name', 'email', 'status', 'action'])
            ->make(true);
    }

    public function showEmployee(Request $request, $id)
    {
        $employee = User::where('id', $id)->first();
        $roles = Role::all();

        return view('admin.employee-show')->with('employee', $employee)
            ->with('roles', $roles);
    }

    public function updateEmployee(Request $request)
    {
        $employee = User::where('id', $request->id)->first();
        $roles = Role::all();

        $employee->name = $request->name;
        $employee->email = $request->email;
        $employee->is_active = $request->is_active;

        switch ($request->rol) {
            case '1':
                $employee->syncRoles('Admin');
                break;
            case '2':
                $employee->syncRoles('Salesman');
                break;
            case '3':
                $employee->syncRoles('Logistic');
                break;
            case '4':
                $employee->syncRoles('Customer');
                break;
            default:
                break;
        }

        if ($request->password) {
            $employee->password = Hash::make($request->password);
        }
        $employee->save();

        // return view('admin.employee-show')
        return redirect()->back()->with('employee', $employee)
        ->with('roles', $roles)
        ->with('success', 'Usario actualizado con exito!');
    }

    public function show(Request $request, $id)
    {
        $company = Company::where('id', $id)->first();
        return view('admin.company-list')->with('id', $id)->with('company', $company);
    }
    
    public function store(storeCompanyRequest $request)
    {
        $image_name = uniqid() . '.' . $request->image->getClientOriginalExtension();
        $image_path = 'uploads/' . $image_name;
        Image::make($request->image)->fit(1024, 768)->save(public_path($image_path));


        Company::create(
            [
            'rif' => $request->rif,
            'company_name' => $request->name,
            'logo_url' => $image_name,
            ]
        );

        return redirect()->back()->with('success', 'Empresa creada con exito!');
    }

    public function changeSeller(Request $request)
    {
        $sellers = User::select('users.*')
        ->join('employees', 'users.id', '=', 'employees.user_id')
        ->join('model_has_roles', function ($join) {
            $join->on('users.id', '=', 'model_has_roles.model_id')
            ->where('model_has_roles.model_type', '=', 'App\\Models\\User');
        })
        ->join('roles', 'model_has_roles.role_id', '=', 'roles.id')
        ->where('roles.name', '=', 'Salesman')
        ->select('employees.id as id', 'users.name as name')
        ->orderBy('users.name', 'asc')
        ->get();
       
        return view('admin.change-seller')
            ->with('sellers', $sellers);
    }

    public function getCustomerFromSeller(Request $request)
    {
        $userList = DB::table('customer_employee')
                    ->where('customer_employee.employee_id', '=', $request->user_id)
                    ->join('customers', 'customer_employee.customer_id', '=', 'customers.id')
                    ->join('users', 'users.id', '=', 'customers.user_id')
                    ->select('users.id as user_id', 'customer_employee.customer_id', 'customer_employee.employee_id as old_seller', 'users.name')
                    ->orderBy('users.name', 'asc')
                    ->get();
        
        return response()->json($userList);
    }

    public function updateCustomerFromEmployee(Request $request)
    {
        
        $roles = Role::all();
        $customerIds = $request->ids;
        $employeeId = $request->seller_to;
        $employeeFrom = $request->seller_from;

        $test = DB::table('customer_employee')
        ->whereIn('customer_id', $customerIds)
        ->where('employee_id', $employeeFrom)
        ->delete();

        foreach ($customerIds as $customerId) {
            DB::table('customer_employee')->insert([
            'customer_id' => $customerId,
            'employee_id' => $employeeId,
            ]);
        }
        
        return response()->json([
                    'roles' => $roles,
                    'success' => 'Usuario actualizado con éxito!',
                ]);
    }

    public function customersList(Request $request)
    {
        $companies = Company::orderBy('company_name', 'asc')->get();
        return view('admin.customer-list')->with('companies', $companies);
    }

    public function getCustomers(Request $request)
    {
        $company = 0;
        if ($request->company) {
            $company = $request->company;
        }

        $data = Customer::join('users', 'customers.user_id', '=', 'users.id')
            ->join('customer_employee', 'customers.id', '=', 'customer_employee.customer_id')
            ->join('employees', 'customer_employee.employee_id', '=', 'employees.id')
            ->where('employees.company_id', '=', $company)
            ->select('users.id', 'customers.rif', 'users.name', 'users.email')
            ->orderBy('users.name', 'asc');
        
        

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($row) {
                $actionBtn = '<a href="'. url('customer/show', $row->id) .'" class="edit btn btn-success btn-sm">Ver</a>';
                return $actionBtn;
            })
            ->rawColumns(['action'])
            ->make(true);
    }

    public function getCustomersSeller(Request $request)
    {

        $currentSeller = what_employee(Auth::user()->id)->id;

        $data = Customer::join('users', 'customers.user_id', '=', 'users.id')
           ->join('customer_employee', 'customers.id', '=', 'customer_employee.customer_id')
           ->where('customer_employee.employee_id', '=', $currentSeller)
           ->select('users.id', 'customers.rif', 'users.name', 'users.email');

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($row) {
                $actionBtn = '<a href="'. url('customer/show', $row->id) .'" class="edit btn btn-success btn-sm">Ver</a>';
                return $actionBtn;
            })
            ->rawColumns(['action'])
            ->make(true);
    }

    public function customerShow(Request $request, $id)
    {
        $customer = User::where('users.id', $id)
             ->join('customers', 'customers.user_id', '=', 'users.id')
             ->select('users.id as user_id', 'customers.id  as customer_id', 'customers.rif', 'customers.phone', 'customers.address', 'users.name', 'users.email')
             ->orderBy('users.name', 'asc')
             ->first();

        $sellers = Employee::whereHas('user', function ($query) {
            $query->where('is_active', 1)
            ->whereHas('roles', function ($query) {
                $query->where('name', 'Salesman');
            });
        })->whereNotIn('id', function ($query) use ($customer) {
            $query->select('employee_id')
                ->from('customer_employee')
                ->where('customer_id', $customer->customer_id);
        })->get();

        return view('admin.customer-show')->with('customer', $customer)
                                            ->with('sellers', $sellers);
    }

    public function updateCustomerEmployee(Request $request)
    {
        $companies = Company::all();
        $userId = $request->user_id;
        $customerId = $request->customer_id;
        $sellersChange = $request->sellers;

        $user = User::where('id', $userId)->first();
        $user->name = $request->name;
        $user->email = $request->email;

        $customer = Customer::where('user_id', $userId)->first();
        $customer->rif = $request->rif;
        $customer->address = $request->address;
        $customer->phone = $request->phone;
        $customer->save();

        if ($request->password) {
            $user->password = Hash::make($request->password);
        }
        $user->save();

        if ($sellersChange) {
            foreach ($sellersChange as $sellerId) {
                DB::table('customer_employee')->insert([
                    'customer_id' => $customerId,
                    'employee_id' => $sellerId,
                ]);
            }
        }
                
        return redirect()->route('customer.show', ['id' => $userId])
                    ->with('success', 'Usuario actualizado con éxito!');
    }

    public function destroySellerFromCustomer(Request $request)
    {
        
        $customerId = $request->input('customerId');
        $sellerId = $request->input('sellerId');
        
        DB::table('customer_employee')
        ->where('customer_id', $customerId)
        ->where('employee_id', $sellerId)
        ->delete();

        return response()->json(['success' => true]);
    }
}
Leave a Comment