<?php

namespace App\Traits;

use App\Models\Order;
use App\Models\BusinessInformation;
use App\Models\Product;
use App\Models\ProductCoupon;

trait OrderTrait{

    public function get_order_invoice()
    {
        $business = BusinessInformation::where('client_id',Auth()->User()->id)->get();
        foreach($business as $b)
        {
            $bid = $b->id;
        }
        $productcoupon = ProductCoupon::where('business_id',$bid)->exists();

        if($productcoupon)
        {
            $business_info = BusinessInformation::select()
            ->join('product_coupons', 'product_coupons.business_id', '=', 'business_information.id')
            ->join('products', 'products.id', '=', 'product_coupons.product_id')
            ->join('orders','orders.client_id','=','business_information.client_id')
            ->join('coupons', 'coupons.id', '=', 'product_coupons.coupon_id')
            ->join('users', 'users.id', '=', 'business_information.client_id')
            ->join('closers', 'closers.id', '=', 'business_information.closerid')
            ->where('business_information.id', '=', $bid)
            ->select(
                'business_information.*',
                'business_information.radius AS bradius',
                'closers.*',
                'orders.ordernumber',
                'orders.order_status',
                'orders.created_at AS date',
                'orders.invoice_id',
                'users.*',
                'coupons.*',
                'product_coupons.new_discount_price AS newprice',
                'products.productname',
                'products.price',
                'products.productdescription'
            )
            ->get();
            return $business_info;
        }
        else{
            $business_info = BusinessInformation::where('business_information.client_id',Auth()->User()->id)
            ->join('users','users.id','=','business_information.client_id')
            ->join('orders','orders.client_id','=','business_information.client_id')
            ->join('products','products.id','=','business_information.pid')
            ->join('closers','closers.id','=','business_information.closerid')
            ->select(
                'business_information.*',
                'business_information.radius AS bradius',
                'closers.*',
                'orders.ordernumber',
                'orders.order_status',
                'orders.created_at AS date',
                'orders.invoice_id',
                'users.*',
                'products.*',
            )
            ->get();
            return $business_info;
        }
       
    }

    public function get_single_product($productid)
    {
        $product = Product::where('id',$productid)->get();
        return $product;
    }

    public function get_business_order($id)
    {
       $bsid = BusinessInformation::where('client_id',$id)->get();
       foreach($bsid as $bid)
       {
        $bidd = $bid->id;
       }
       $product_coupon = ProductCoupon::where('business_id',$bidd)->get();
       $order_detail = null;
       foreach($product_coupon as $pc)
       {
       $order_detail = Order::select()
       ->join('business_information','business_information.client_id','=','orders.client_id')
       ->join('users','users.id','=','business_information.client_id')
       ->join('products','products.id','=','business_information.pid')
       ->join('closers','closers.id','=','business_information.closerid')
       ->join('product_coupons','product_coupons.business_id','=','business_information.id')
       ->join('coupons','coupons.id','=','product_coupons.coupon_id')
       ->where('orders.client_id','=',$id)
       ->select('business_information.bname',
       'business_information.bphone',
       'business_information.bemail',
       'business_information.radius',
       'business_information.radius_image',
       'business_information.radius_address',
       'business_information.baddress',
       'business_information.services',
       'orders.id AS order_id',
       'orders.ordernumber',
       'orders.order_status',
       'orders.created_at',
       'orders.invoice_id',
       'products.productname',
       'products.productdescription',
       'products.price AS price',
       'product_coupons.new_discount_price AS newprice',
       'users.id AS userid',
       'users.first_name',
       'users.last_name',
       'users.email',
       'users.phone',
       'users.address',
       'closers.closer_name',
       'coupons.code',
       'coupons.discount_price'
        )
       ->get();
       if ($product_coupon->isNotEmpty()) {
        break;
    }
    }
    if ($product_coupon->isNotEmpty()) {
        return $order_detail;
    } 
    else {
        $order_detail2 = Order::select()
       ->join('business_information','business_information.client_id','=','orders.client_id')
       ->join('users','users.id','=','business_information.client_id')
       ->join('products','products.id','=','business_information.pid')
       ->join('closers','closers.id','=','business_information.closerid')
       ->where('orders.client_id','=',$id)
       ->select('business_information.bname',
       'business_information.bphone',
       'business_information.bemail',
       'business_information.radius',
       'business_information.radius_image',
       'business_information.radius_address',
       'business_information.baddress',
       'business_information.services',
       'orders.id AS order_id',
       'orders.ordernumber',
       'orders.order_status',
       'orders.created_at',
       'orders.invoice_id',
       'products.productname',
       'products.productdescription',
       'products.price AS price',
       'users.id AS userid',
       'users.first_name',
       'users.last_name',
       'users.email',
       'users.phone',
       'users.address',
       'closers.closer_name',
        )
       ->get();

        return $order_detail2;
    }
    }

}