Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
6.4 kB
1
Indexable
Never
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use App\Models\Package;
use App\Models\PackageServiceType;
use App\Models\PackageItem;
use App\Models\PackagePhoto;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;


class AdminPackagesController extends Controller
{
    public function index()
    {
        return view('admin.packages.index');
    }

    public function create()
    {
        // Fetch approved records from customer_preregister_pdfs
        $approvedCustomers = DB::table('customer_preregister_pdfs')
            ->join('users', 'customer_preregister_pdfs.customer_id', '=', 'users.id')
            ->where('customer_preregister_pdfs.status', 'approved')
            ->select('users.id', 'users.first_name')
            ->get();

        return view('admin.packages.create', compact('approvedCustomers'));
    }

    public function getCustomerCountry(Request $request)
    {
        $customer = User::find($request->customer_id);
        
        if ($customer) {
            return response()->json(['country' => $customer->country]);
        } else {
            return response()->json(['country' => null]);
        }
    }

    public function store(Request $request)
    {
        $request->validate([
            'package_weight' => 'required',
            'package_length' => 'required',
            'package_width' => 'required',
            'package_height' => 'required',
            'package_source' => 'required',
            'package_total_price' => 'required',
            'country' => 'required',
            'package_product_type' => 'required',
            'photo_paths' => 'nullable|array',
            'photo_paths.*' => 'nullable|file|image|mimes:jpeg,png,jpg,gif|max:102400',
        ]);

        // Save package
        $package = new Package();
        $package->packageID = 'PKG' . strtoupper(uniqid());
        $package->package_weight = $request->package_weight;
        $package->package_length = $request->package_length;
        $package->package_width = $request->package_width;
        $package->package_height = $request->package_height;
        $package->package_source = $request->package_source;
        $package->package_total_price = $request->package_total_price;
        $package->country = $request->country;
        $package->package_product_type = $request->package_product_type;
        $package->customer_id = $request->customer_id;
        $package->status = $request->status;
        $package->save();

        // Save service fields rows
        if ($request->has('package_service_name')) {
            foreach ($request->package_service_name as $index => $serviceName) {
                $serviceType = new PackageServiceType();
                $serviceType->package_id = $package->id;
                $serviceType->package_service_name = $serviceName;
                $serviceType->express_shipping_price = $request->express_shipping_price[$index];
                $serviceType->handling_price = $request->handling_price[$index];
                $serviceType->insurance_price = $request->insurance_price[$index];
                $serviceType->delivery_time = $request->delivery_time[$index];

                // Directory path for storing logos
                $directory = public_path('storage/package-service-logos');

                // Create the directory if it doesn't exist
                if (!file_exists($directory)) {
                    mkdir($directory, 0777, true);  // Creates the directory with the correct permissions
                }

                // Handle file upload and saving the path
                if ($request->hasFile('package_service_logo') && isset($request->package_service_logo[$index])) {
                    $logo = $request->file('package_service_logo')[$index];
                    $filename = time() . '-' . $logo->getClientOriginalName();  // Generate a unique filename
                    $logo->move($directory, $filename);  // Move the file to the directory
                    $serviceType->package_service_logo = 'storage/package-service-logos/' . $filename;  // Save the path
                }

                $serviceType->save();
            }
        }


        // Save package items
        if ($request->has('item_name')) {
            foreach ($request->item_name as $index => $itemName) {
                $item = new PackageItem();
                $item->package_id = $package->id;
                $item->item_name = $itemName;
                $item->item_price = $request->item_price[$index];
                $item->package_value = $request->package_value[$index];
                $item->item_quantity = $request->item_quantity[$index];
                $item->save();
            }
        }

        // Save photos in package_photos table
        if ($request->hasFile('photo_paths')) {
            foreach ($request->file('photo_paths') as $photo) {
                $path = $photo->store('package-photos', 'public');
                $packagePhoto = new PackagePhoto();
                $packagePhoto->package_id = $package->id;
                $packagePhoto->photo_path = $path;
                $packagePhoto->save();
            }
        }

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



    public function uploadPhotos(Request $request)
    {
        $request->validate([
            'photo_paths.*' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:102400',
        ]);

        $paths = [];
        $directory = public_path('storage/package-photos'); 

        if (!file_exists($directory)) {
            mkdir($directory, 0777, true); 
        }

        if ($request->hasFile('photo_paths')) {
            foreach ($request->file('photo_paths') as $photo) {
                $filename = time() . '-' . $photo->getClientOriginalName();  
                $photo->move($directory, $filename);  
                $paths[] = 'storage/package-photos/' . $filename;  
            }
        }

        return response()->json(['paths' => $paths], 200);
    }



}
Leave a Comment