GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/api.ayokah.co.uk/app/Http/Controllers/Seller/SellerAccountController.php
<?php

namespace App\Http\Controllers\Seller;

use App\Models\User;
use App\Models\Seller;
use App\Models\Category;
use App\Mail\MailNewShop;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Services\ActivityLogger;
use App\Http\Controllers\Controller;
use App\Models\Wallet;
use App\Models\WalletHistory;
use Illuminate\Support\Facades\Mail;

class SellerAccountController extends Controller
{
    public function shop(Request $request)
    {
        try {
            // Validate the request
            $validatedData = $request->validate([
                'user_id' => 'required|exists:sellers,user_id',
            ]);

            // Fetch the shop record
            $shop = Seller::where('user_id', $validatedData['user_id'])->first();

            // Check if the shop exists
            if ($shop === null) {
                return response()->json(['status' => 'error', 'message' => 'No shop found. Setup your store'], 404);
            }

            // Return the shop data
            return response()->json(['status' => 'success', 'data' => $shop], 200);
        } catch (\Throwable $th) {
            // Return error response if exception occurs
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
        }
    }

    public function getCategory()
    {
        try {
            $food = Category::whereNull('parent_id')->where('type', 'foods')->get();
            $service = Category::whereNull('parent_id')->where('type', 'services')->get();
            return response()->json([
                'status' => 'success',
                'data' => [
                    'food' => $food,
                    'service' => $service,
                ],
            ], 200);
        } catch (\Throwable $th) {
            return response()->json([
                'status' => 'error',
                'message' => $th->getMessage(),
            ], 500);
        }
    }

    public function saveShop(Request $request)
    {
        // Check if the user already has a seller profile
        $check = Seller::where('user_id', $request->user_id)->first();
        if ($check) {
            return response()->json(['status' => 'error', 'message' => 'You already have a business profile'], 401);
        }
        try {
            $validatedData = $request->validate([
                'user_id' => 'required|exists:users,id',
                'category_id' => 'required|string|max:100', 

                'business_name' => 'required|string|max:100|unique:sellers,business_name',
                'description' => 'required|string|max:1000',
                'reg_no' => 'nullable|string|max:100|unique:sellers,reg_no',
                'type' => 'required|string|max:100',

                'logo' => 'required|url',
                'logo_public_id' => 'required|string',
                'cover_image' => 'required|url',
                'cover_image_public_id' => 'required|string',

                'facebook' => 'nullable|url|max:255|unique:sellers,facebook',
                'instagram' => 'nullable|url|max:255|unique:sellers,instagram',

                'sort_code' => 'nullable|string',
                'account_no' => 'nullable|string|max:20|unique:sellers,account_no',
                'account_name' => 'nullable|string',
                'bank_name' => 'nullable|string|max:100',

                'device_name' => 'required|string',
            ]);
            // Create a new Seller record
            $seller = new Seller();
            $seller->user_id = $validatedData['user_id'];
            $seller->category_id = $validatedData['category_id'];
            // $seller->address = $validatedData['address'];
            // $seller->postcode = $validatedData['postcode'];
            // $seller->state = $validatedData['state'];
            // $seller->city = $validatedData['city'];
            // $seller->country = $validatedData['country'];
            // $seller->longitude = $validatedData['longitude'];
            // $seller->latitude = $validatedData['latitude'];
            $seller->business_name = ucwords(strtolower(trim($validatedData['business_name'])));
            $seller->slug = Str::slug($validatedData['business_name']);
            $seller->description = $validatedData['description'];
            $seller->reg_no = $validatedData['reg_no'];
            $seller->type = $validatedData['type'];
            $seller->logo = $validatedData['logo'];
            $seller->logo_public_id = $validatedData['logo_public_id'];
            $seller->cover_image = $validatedData['cover_image'];
            $seller->cover_image_public_id = $validatedData['cover_image_public_id'];
            $seller->facebook = $validatedData['facebook'];
            $seller->instagram = $validatedData['instagram'];
            $seller->sort_code = $validatedData['sort_code'];
            $seller->account_no = $validatedData['account_no'];
            $seller->account_name = $validatedData['account_name'];
            $seller->bank_name = $validatedData['bank_name'];
            $seller->status = 'inactive';

            // Save the seller record
            $seller->save();

            // Log the user activity
            $device = $validatedData['device_name'];
            $user_id = $validatedData['user_id'];
            $user = User::find($user_id);
            //send email to the seller to welcome them using their email and name
            $email = $user->email;
            $name = $user->name;
            Mail::to($email)->send(new MailNewShop($name));

            $user_role = 'seller';
            // Log the user activity
            $device = $validatedData['device_name'];
            $activityLogger = app(ActivityLogger::class);
            $clientIp = request($key = null, $default = null)->ip();
            $activityLogger->log($clientIp, 'Seller Create Shop from app (API)', $user->id, $user_role, $device);
            return response()->json(['status' => 'success', 'message' => 'Your selling shop was created successfully', 'data' => $seller], 200);
        } catch (\Throwable $th) {
            info('unable to setup shop '. $th->getMessage());
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
        }
    }

    public function updateShopAddress(Request $request)
    {
        try {
            // Validate the request data
            $validatedData = $request->validate([
                'user_id' => 'required|exists:sellers,user_id',
                'address' => 'required|string|max:255',
                'postcode' => 'nullable|string',
                'state' => 'required|string',
                'city' => 'nullable|string',
                'country' => 'required|string|max:100',
                'longitude' => 'required|max:100',
                'latitude' => 'required|max:100',
            ]);

            // Find the seller by user_id
            $seller = Seller::where('user_id', $validatedData['user_id'])->first();

            if ($seller) {
                // Update the seller's address details
                $seller->update([
                    'address' => $validatedData['address'],
                    'postcode' => $validatedData['postcode'],
                    'state' => $validatedData['state'],
                    'city' => $validatedData['city'],
                    'country' => $validatedData['country'],
                    'longitude' => $validatedData['longitude'],
                    'latitude' => $validatedData['latitude'],
                ]);

                // Respond with success
                return response()->json([
                    'status' => 'success',
                    'message' => 'Address updated successfully',
                    'data' => $seller
                ], 200);
            } 
        } catch (\Throwable $th) {
            return response()->json([
                'status' => 'error',
                'message' => $th->getMessage()
            ], 500);
        }
    }

    public function updateShopStatus(Request $request)
    {
        try {
            $data = $request->validate([
                'user_id' => 'required|exists:sellers,user_id',
            ]);

            $seller = Seller::where('user_id', $data['user_id'])->first();

            $seller->status = 'active';
            $seller->save();

            return response()->json(['status' => 'success', 'message' => 'Shop status updated successfully!', 'data' => $seller], 200);
        } catch (\Throwable $th) {
            info($th->getMessage());
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
        }
    }


    public function sellerWallet(Request $request)
    {
        try {
            // Validate the request
            $validatedData = $request->validate([
                'user_id' => 'required|exists:sellers,user_id',
            ]);

            // Fetch the wallet record
            $wallet = Wallet::where('user_id', $validatedData['user_id'])->first();

            // Check if wallet exists and return appropriate response
            if ($wallet) {
                return response()->json(['status' => 'success', 'message' => 'Wallet fetched successfully', 'data' => $wallet], 200);
            } else {
                return response()->json(['status' => 'error', 'message' => 'Wallet not found', 'data' => []], 404);
            }
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
        }
    }
    public function sellerWalletHistory(Request $request)
    {
        try {
            // Validate the request
            $validatedData = $request->validate([
                'user_id' => 'required|exists:sellers,user_id',
            ]);

            // Fetch the wallet record
            $wallet = WalletHistory::with('seller')->where('user_id', $validatedData['user_id'])->get();

            // Check if wallet exists and return appropriate response
            if ($wallet) {
                return response()->json(['status' => 'success', 'message' => 'Wallet history fetched successfully', 'data' => $wallet], 200);
            } else {
                return response()->json(['status' => 'error', 'message' => 'Wallet not found', 'data' => []], 404);
            }
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
        }
    }

    public function allShop()
    {
        try {
            $shop = Seller::latest()->get();
            return response()->json(['status' => 'success', 'data' => $shop], 200);
        } catch (\Throwable $th) {
            return response()->json(['status' => 'error', 'message' => $th->getMessage()], 404);
        }
    }
}