0

I Create Transport Booking System in Laravel. When Customer Book a New Trip to Transport i calculate Commission Amount using Event (CalculateCustomerBookingolCharges).

I try to book a new trip, all data's has been save but Commission Amount Not Calculated (Event Not Triggered).

Here is my Controller Code :

public function savenewbooking (Request $request) 
{   
    $createbooking=Booking::create([
        'order_id' => $orderid,
        'token' => $request->input('_token'),
        'booking_id' => $curbookingid,
        'invoice_prefix' => $bookingpre,
        'userid' => $this->userid,
        'user_email' => $customerData->email,
        'trip_amt' => $tripamt,
        'extra_amt' => $totalextraserviceamount,
        'total_amt' => $totalbookingamount,
        'ip_address' => $ipaddress,
        'booking_status' => 1,
        'created_by' => $this->userid
   ]);

   event(new CalculateCustomerBookingolCharges($createbooking));

   //Event::fire(new CalculateCustomerBookingolCharges($createbooking));

   return response()->json([
       'items' => $orderid,
       'error '=> [
            'code' => 'REQUEST_SUCCESSFUL',
            'description'=>'Booking Confirmed'
        ]
   ],200)

}

Event Listeners :

<?php

namespace App\Listeners;

use App\Events\CalculateCustomerBookingolCharges;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Booking;
use App\Appsettings;
use Auth;

class CustomerBookingolCharges
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CalculateCustomerBookingolCharges  $event
     * @return void
     */
    public function handle(CalculateCustomerBookingolCharges $event)
    {


        $ourlorrypercentage=Appsettings::Where('config_title','ourlorry_comission')->pluck('config_modified')->first();

        $ourlorryamt=($ourlorrypercentage/100)*$event->booking->total_amt;

        $updatebooking=Booking::Where('id',$event->booking->id)->update(['ourlorry_percentage'=>$ourlorrypercentage,'ourlorry_amount'=>$ourlorryamt,'updated_by'=>Auth::user()->id]);

    }
}
Daniel
  • 2,621
  • 2
  • 18
  • 34
Karthik
  • 5,589
  • 18
  • 46
  • 78
  • could you please show the CalculateCustomerBookingolCharges event/notification file? – Sachin Kumar Jun 08 '18 at 11:30
  • Make sure you have configured the broadcasting setting in `app.php` and the event is implemented with ShouldBroadcast. – Nitish Kumar Jun 08 '18 at 11:31
  • A random side note until you give more code. Wouldn't it be better to call the event "CustomerBookingolCharges" and the Listener "CalculateCu...."? – Inuyaki Jun 08 '18 at 12:30

1 Answers1

1

Make sure to register your listener to listen to it's corresponding event in the EventServiceProvider

Go to App\Providers\EventServiceProvider you will find protected array called listen

protected $listen = [

] 

In your case your event could be registered as shown below

protected $listen = [
    'App\Events\CalculateCustomerBookingolCharges' => [
        'App\Listeners\CustomerBookingolCharges',
    ],
];

Hope that solves the problem.

Mohammad Istanboli
  • 755
  • 1
  • 8
  • 19