Current File : //home/tradevaly/www/app/Http/Controllers/User/CheckOutController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Models\User;
use App\Models\Country;
use App\Models\Order;
use App\Models\Product;
use App\Models\VendorOrder;
use App\Models\OrderItem;
use App\Models\Transaction;
use App\Models\AdminNotification;
use App\Models\ProductInvoices;
use Brian2694\Toastr\Facades\Toastr;
use App\Notifications\OrderSuccess;
use Illuminate\Support\Facades\Notification;
use Illuminate\Http\Request;
use Carbon\Carbon;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;

class CheckOutController extends Controller
{
    public function index(){
        $user = User::where('id',auth()->id())->first();
        $countries = Country::all();
        $carts = Cart::where('user_id',auth()->id())->get();
        if($carts->count()<1)
        {
            Toastr::warning('Please select a product');
            return redirect()->route('products');
        }else{
        return view('user.pages.product.checkOut',compact('carts','user','countries'));
        }
    }
    
    public function checkout(Request $request)
    {
        
        $carts = Cart::where('user_id',auth()->id())->get();
        if($carts->count()<1)
        {
            Toastr::warning('There is No item in cart');
            return redirect()->route('products');
        }
        else
        {
            $request->validate([
                'name'=>'required',
                'phone'=>'required|min:9',
                'city'=>'required',
                'country'=>'required',
                'address_1'=>'required',
                'email'=>'required|email',
            ]);
        
            $name = $request->name;
            $phone = $request->phone;
            $email = $request->email;
            $city = $request->city;
            $country = $request->country;
            $payment_type= $request->payment_type;
            $address_1 = $request->address_1;
            $address_2 = $request->address_2;
            
            $randomText = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 7);
            $order_id = rand(100000, 999999).$randomText;
            $total_amount = 0;
            $total_discount = 0;
            $total_quantity = $carts->sum('quantity');
            
             // calculate total
            foreach($carts as $cart)
            {
                $total = ($cart->product->price * $cart->quantity);
                $total_amount = $total_amount+ $total;
                $total_discount = $total_discount + (($cart->product->discount_price/100) * $total);
            }
            
            
            // save transaction
            $transaction = new Transaction();
            $transaction->order_id = $order_id;
            $transaction->total_amount = $total_amount;
            $transaction->discount_amount = $total_discount;
            $transaction->user_id = auth()->id();
            $transaction->payment_type = $payment_type;
            $transaction->order_from = 'product';
            $transaction->order_status = 'new';
            $transaction->save();
            
    
            // save order 
            $order = new Order();
            $order->order_id = $order_id;
            $order->total_amount = $total_amount;
    		$order->total_discount=$total_discount;
            $order->total_qty = $total_quantity;
            $order->payment_status = "unpaid";
            $order->payment_type = $payment_type;
            $order->transaction_id = $transaction->id;
            $order->buyer_id = auth()->id();
            $order->name= $name;
            $order->email=$email;
            $order->address_1= $address_1;
            $order->address_2= $address_2;
            $order->country_id= $country;
            $order->city=$city;
            $order->phone= $phone;
            $order->save();
            
            // save order items
            foreach ($carts as $cart)
            {
            $order_item = new OrderItem();
            $order_item->order_id = $order_id;
            $order_item->product_id= $cart->product_id;
            $order_item->product_title=$cart->product->title;
            $order_item->product_price=$cart->product->price;
            $order_item->discount_price=$cart->product->discount_price/100 * $cart->product->price;
            $order_item->quantity=$cart->quantity;
            $order_item->vendor_id=$cart->product->supplier_id;
            $order_item->status='new';
            $order_item->save();
            
            
            $vendor = VendorOrder::where('order_id',$order_id)->where('vendor_id',$cart->product->supplier_id)->first();
            if(isset($vendor))
            {
                $total = $vendor->total_amount + ($cart->product->price * $cart->quantity);
                $vendor->total_amount = $total;
                
                $discount = $vendor->total_discount + ((($cart->product->discount_price/100)*($cart->product->price*$cart->quantity)));
                $vendor->total_discount = $discount;
                $vendor->save();
            }else
            {
                $vendor_order = new VendorOrder();
                $vendor_order->order_id=$order_id;
                $vendor_order->vendor_id=$cart->product->supplier_id;
                $vendor_order->total_amount=$cart->product->price * $cart->quantity;
                $vendor_order->total_discount=(($cart->product->discount_price/100)*($cart->product->price*$cart->quantity));
                $vendor_order->status='new';
                $vendor_order->save();
                
                
                $notification = New AdminNotification();
                $notification->user_id=auth()->id();
                $notification->type="order";
                $notification->message="Order a New Product";
                $notification->admin_reciver="yes";
                $notification->reciver=$cart->product->supplier_id;
                $notification->data=$order_id;
                $notification->save();
            }
            
            $cart->delete();
            }
            $invoice = new ProductInvoices();
            $invoice->order_id = $order_id;
            $invoice->buyer_id = auth()->id();
            $invoice->amount = $total_amount;
            $invoice->discount_price = $total_discount;
            $invoice->status = "unpaid";
            $invoice->transaction_id=$transaction->id;
            $invoice->payment_method=$payment_type;
            $invoice->payment_type=$payment_type;
            $invoice->payment_date = Carbon::now();
            $invoice->save();
            
            if($payment_type=="online")
            {
            $info = array(
                    'currency' => 'BDT',
                    // 'amount' => $total_amount - $total_discount,
                    'amount' => 1,
                    'order_id' => $order_id,
                    'discsount_amount' => 0,
                    'disc_percent' => 0,
                    'client_ip' => 'N\L',
                    'customer_name' => $name,
                    'customer_phone' => $phone,
                    'email' => $email,
                    'customer_address' => $address_1,
                    'customer_city' => $city,
                    'customer_state' => "N/L",
                    'customer_postcode' => "N/L",
                    'customer_country' => Country::find($country)->name
                );
            $shurjopay_service = new ShurjopayController();
            return $shurjopay_service->checkout($info);
            }
            else{
            $invoiceData = [
                'name' => auth()->user()->name,
                'amount' => $total_amount,
                'discount' => $total_discount,
                'payment' =>"unpaid",
                'order_number' => $order_id,
                'discount' => 0,
                'transaction_id' => $transaction->id,
                ];
            Notification::route('mail', 'rakib042002@gmail.com')->notify(new OrderSuccess($invoiceData));
            // Notification::route('mail', $email)->notify(new OrderSuccess($invoiceData));
            // Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new OrderSuccess($invoiceData));
    
            Toastr::success('Order Successfully Placed');
            return redirect()->route('products');
            }
        }
    }
    
    public function buy($id)
    {
        $iscart = Cart::where('user_id',auth()->id())->where('product_id',$id)->count();
        
        if(auth()->user()->type == "seller"){
            Toastr::warning('Warning!','You Cant buy your own product!');
            return back();
        }else{
            if($iscart>0)
            {
                $cart = Cart::where('user_id',auth()->id())->where('product_id',$id)->latest()->first();
                $cart->quantity = $cart->quantity + 1;
                $cart->save();
                return redirect()->route('user.check.index');
            }
            else{
            $cart = new Cart();
            $cart->product_id = $id;
            $cart->user_id = auth()->id();
            $cart->quantity = 1;
            $cart->save();
            return redirect()->route('user.check.index');
            }
        }
    }
}