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

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\VendorOrder;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\AdminNotification;
use App\Models\SellingProgress;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Carbon\Carbon;

class OrderController extends Controller

{
    public function index(){
        $orders = VendorOrder::where('vendor_id',auth()->id())->orderby('created_at','desc')->latest()->paginate('20');
        return view('user.pages.order.index',compact('orders'));
    }
    public function new(){
        
        $orders = VendorOrder::where('vendor_id',auth()->id())->where('status','new')->latest()->paginate('20');
        return view('user.pages.order.new',compact('orders'));
    }
    public function pending(){
        $orders = VendorOrder::where('vendor_id',auth()->id())->where('status','pending')->latest()->paginate('20');
        return view('user.pages.order.pending',compact('orders'));
    }
    public function delevered(Request $request){
        $orders = VendorOrder::where('vendor_id',auth()->id())->where('status','delevered')->latest()->paginate('20');
        return view('user.pages.order.delevered',compact('orders'));
    }
    public function cancel(Request $request){
        $orders = VendorOrder::where('vendor_id',auth()->id())->where('status','cancel')->latest()->paginate('20');
        return view('user.pages.order.cancel',compact('orders'));
    }
    public function view($id){
        $order = Order::where('order_id',$id)->firstOrFail();
        $items = OrderItem::where('order_id',$id)->where('vendor_id',auth()->id())->get();
        $vendor_order = VendorOrder::where('order_id',$id)->where('vendor_id',auth()->id())->first();
        $notification = AdminNotification::where('data',$id)->where('reciver',auth()->id())->first();
        if(isset($notification))
        {
            if($notification->seen == '')
            {
                $notification->seen= Carbon::now();
                $notification->save();
            }
        }
        return view('user.pages.order.view',compact('order','items','vendor_order'));
    }
    public function status(Request $request,$id)
    {
        $order_item = OrderItem::where('id',$id)->where('vendor_id',auth()->id())->firstOrFail();
        $vendor_order = VendorOrder::where('order_id',$order_item->order_id)->where('vendor_id',auth()->id())->firstOrFail();
        if($vendor_order->status !== "delevered")
        {
        $order_item->status=$request->status;
        $order_item->save();
        
        $vendor_order->status=$request->status;
        $vendor_order->save();
        if($request->status == "delevered")
        {
            $selling_progress = new SellingProgress();
            $selling_progress->seller_id = $vendor_order->vendor_id;
            $selling_progress->order_number = $vendor_order->order_id;
            $selling_progress->payment_status = "pending";
            $selling_progress->amount= $vendor_order->total_amount-$vendor_order->total_discount;
            $selling_progress->order_date =$vendor_order->created_at;
            $selling_progress->amount_relese_date =	Carbon::now()->addDays(7);
            $selling_progress->save();
            
        }
        Toastr::success('Status Updated Successfully');
        }else {
            $order_item->status="delevered";
            $order_item->save();
            Toastr::Error('You cant Update Delevered to others');
        }
        
        return back();
    }
    public function buying()
    {
        $orders = Order::where('buyer_id',auth()->id())->latest()->paginate(20);
        return view('user.pages.order.buying',compact('orders'));
    }
    public function details($id)
    {
        $order = Order::where('order_id',$id)->where('buyer_id',auth()->id())->firstOrFail();
        $items = OrderItem::where('order_id',$id)->get();
        $vendor_order = VendorOrder::where('order_id',$id)->first();
        return view('user.pages.order.buying_view',compact('items','order','vendor_order'));
    }
}