Current File : /home/tradevaly/public_html/app/Http/Controllers/Frontend/AdvertiserController.php
<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use App\Models\Page;
use App\Models\Tag;
use App\Models\BuyOffer;
use App\Models\Country;
use App\Models\User;
use App\Models\Product;
use App\Models\Banner;
use App\Models\AdvertiserOrder;
use Auth;
use App\Models\Transaction;
use Mail;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;
use App\Notifications\User\Advertisement\AdvertismentCreateInvoice;
use Illuminate\Support\Facades\Notification;

class AdvertiserController extends Controller
{
    public function index(){
        
        $data['pages'] = Page::where('slug', 'buyer')->firstOrFail();
        $data['country'] = Country::all();
        $users =  User::where('type','buyer')->select('country_id')->get()->take(8)->toArray(); 
        $country = array();
        foreach($users as $user){
            array_push( $country,$user['country_id']);
        }
        $data['buyers'] =  array_unique($country);
        $data['buyoffer'] = BuyOffer::latest()->take(20)->get();
        $data['tags'] = Tag::orderBy('views', 'desc')->take(10)->get();
        // $data['tagsbyword'] = Tag::orderBy('views', 'desc')->get();
        $data['tagsbyword'] = Product::where('status',1)->latest()->get();
        // $data['tagsbyword'] = Category::where('parent_id','!=', 0)->get();
        // return $data['tagsbyword'];
        $data['banners'] = Banner::where('position','top_buyer_banner')->orWhere('position','bottom_buyer_banner')->latest()->get();
       return view('frontend.pages.advertiser.index')->with($data); 
    }
    
    
    
   
   public function rent(Request $request, $slug){

      $request->validate([
          'check_status' => 'required',
          'g-recaptcha-response' => 'required',
      ]);
      $user  = Auth::user();
      
    //   if (strlen($user->company->phone) === 9) {
    //     Toastr::error('Error!', 'You Have To Provide Your Number go to Company Setting and update!');
    //      return back();
    //   }
      
    
    
       $amount = 0;
       $advertiser_Order = new  AdvertiserOrder();
        if(!settings()->$slug){
              Toastr::success('Something went wrong', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return back(); 
        }
        
        if($slug == 'category_banner'){
           $advertiser_Order->category_id = $request->category_id;
        }
           $discount_name = $slug.'_discount';
           dd ($discount_name);
           $amount =  settings()->$slug * $request->duration;
           $discount_price = $amount /100* number_format(settings()->$discount_name);
           $amount = $amount - $discount_price;
           
        if($request->currency == 'BDT'){
                $currency = 'bdt';
                $amount = $amount * settings()->bdt_rate;
            }
            
            
         
         
         $order_id = rand(100000, 999999);
         
         $advertiser_Order->currency = 'bdt';
         $advertiser_Order->advertiser_name = $slug;
         $advertiser_Order->user_id = $user->id;
         $advertiser_Order->duration = $request->duration;
         $advertiser_Order->amount = $amount;
         
         
         
         // new transaction
        
         $transaction = new Transaction();

        
        $transaction->discount_amount = $discount_price;
        $transaction->total_amount = $amount;
        $transaction->user_id = $user->id;
        $transaction->payment_type = 'online';
        $transaction->order_from = 'advertiser';
        $transaction->order_status = 'new';
        $transaction->save();
        
        // create order id
       $order_id = 'tv-'.rand(1000, 9999).$transaction->id;
         
         
         $advertiser_Order->order_id = $order_id;
         $advertiser_Order->save();
         
         $transaction->order_id = $order_id;
         $transaction->save();
         
        $info = array(
                'currency' => 'bdt',
                'order_for' => 'advertiser',
                'amount' => 1,
                // 'amount' => $amount,
                'order_id' => $order_id, 
                'discsount_amount' => 0,
                'disc_percent' => 0,
                'client_ip' => "N/L",
                'customer_name' => Auth::user()->name,
                'customer_phone' => ((Auth::user()->company->phone)? Auth::user()->company->phone:'+0170000000'),
                'email' => Auth::user()->email,
                'customer_address' => (Auth::user()->address? Auth::user()->address:'N\L'),
                'customer_city' => "N/L",
                'customer_state' => "N/L",
                'customer_postcode' => "N/L",
                'customer_country' => Auth::user()->country->name
            );
            
             $invoiceData = (object) [
                'id' => $advertiser_Order->id,
                'name' => $user->name,
                'order_id' => $order_id,
                'amount' => $amount,
                'payment_status' => 'pending',
                'discount' => $discount_price,
                'transaction_id' => $transaction->id,
                'advertisment_place' => $advertiser_Order->advertiser_name,
                'duration' => $advertiser_Order->duration,
                'created_at' => $transaction->created_at,
            ];
            
             Notification::send($user, new AdvertismentCreateInvoice($invoiceData));
             Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new AdvertismentCreateInvoice($invoiceData));
            
            $shurjopay_service = new ShurjopayController();return $shurjopay_service->checkout($info);
        
   }
    public function advertiser_buy_preview(Request $request,$slug){
       
        $discount_name = $slug.'_discount';
       $data['duration'] = $request->duration;
       $data['link'] = $request->link;
       $data['amount'] = settings()->$slug;
       $data['slug'] = $slug;
       $data['discount'] = settings()->$discount_name;
        $data['pages'] = Page::where('slug', 'apply')->firstOrFail();
        return view('frontend.pages.advertiser.buy_view')->with($data);
    }
    
}