Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Models\Booking;
- use App\Models\Transaction;
- use Illuminate\Http\Request;
- use App\Utility;
- use App\Models\Flight;
- use App\Models\Airport;
- use App\Models\Airline;
- use App\Models\Customer;
- use App\Models\FareFamily;
- use App\Models\FlightType;
- use DataTables;
- use DB;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Redirect;
- use Hashids;
- use App\Classes\SmsApi;
- class BookingController extends Controller
- {
- protected $utility;
- protected $model;
- protected $transactionModel;
- protected $airlineModel;
- protected $airportModel;
- protected $flightModel;
- protected $customerModel;
- protected $fareFamilyModel;
- protected $flightTypeModel;
- protected $table;
- protected $flightTable;
- protected $airlineTable;
- protected $airportTable;
- protected $customerTable;
- protected $fareFamilyTable;
- protected $flightTypeTable;
- protected $transactionTable;
- protected $route;
- protected $createPage;
- protected $listPage;
- protected $showPage;
- protected $smsApi;
- public function __construct()
- {
- $this->model = new Booking();
- $this->flightModel = new Flight();
- $this->transactionModel = new Transaction();
- $this->airlineModel = new Airline();
- $this->airportModel = new Airport();
- $this->customerModel = new Customer();
- $this->fareFamilyModel = new FareFamily();
- $this->flightTypeModel = new FlightType();
- $this->table = $this->model->getTable();
- $this->transactionTable = $this->transactionModel->getTable();
- $this->airlineTable = $this->airlineModel->getTable();
- $this->airportTable = $this->airportModel->getTable();
- $this->flightTable = $this->flightModel->getTable();
- $this->customerTable = $this->customerModel->getTable();
- $this->fareFamilyTable = $this->fareFamilyModel->getTable();
- $this->flightTypeTable = $this->flightTypeModel->getTable();
- $this->route = Utility::bookingRoute();
- $this->createPage = 'pages.booking_create';
- $this->listPage = 'pages.booking_list';
- $this->showPage = 'pages.booking_show';
- $this->smsApi = new SmsApi();
- }
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
- // dd($this->smsApi->sendMessage('01858023977','Api testing message',) );
- //dd($request->input('draw'));
- if ($request->ajax() || $request->input('draw') == 1) {
- $data = $this->model::select(
- //Booking Info
- [$this->table . '.id as bookingId',
- $this->table . '.flight',
- $this->table . '.customer',
- $this->table . '.booking_date',
- // $this->table . '.cabin_class',
- $this->table . '.fare_family',
- $this->table . '.fare_price',
- $this->table . '.is_cancelled',
- $this->table . '.refund_reason',
- $this->table . '.status as bookingStatus',
- //Customer Info
- $this->customerTable.'.id as customerId',
- $this->customerTable.'.title',
- $this->customerTable.'.first_name',
- $this->customerTable.'.last_name',
- $this->customerTable.'.dob',
- $this->customerTable.'.email',
- $this->customerTable.'.phone1',
- $this->customerTable.'.phone2',
- $this->customerTable.'.phone3',
- DB::Raw("CONCAT({$this->customerTable}.title,'. ', {$this->customerTable}.first_name, ' ' , {$this->customerTable}.last_name) as CustomerFullName"),
- //Fair Family Info
- $this->fareFamilyTable.'.name as fairFamilyName',
- // Flight Info
- // $this->flightTable.'.title as FlightTitle',
- $this->flightTable.'.airline',
- $this->flightTable.'.fare_type',
- $this->flightTable.'.flight_from',
- $this->flightTable.'.flight_to',
- DB::Raw("(select `{$this->airportTable}`.`name` from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_from`) as fromName"),
- DB::Raw("(select `{$this->airportTable}`.`name` from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_to`) as toName"),
- $this->flightTable.'.take_of_time',
- $this->flightTable.'.landing_time',
- $this->flightTable.'.flight_date',
- //Airline Info
- $this->airlineTable.'.name as AirlineName'
- ]
- )
- ->leftJoin($this->customerTable, function($join) {
- $join->on($this->table.'.customer', '=', $this->customerTable.'.id');
- })
- ->leftJoin($this->fareFamilyTable, function($join) {
- $join->on($this->table.'.fare_family', '=', $this->fareFamilyTable.'.id');
- })
- ->leftJoin($this->flightTable, function($join) {
- $join->on($this->table.'.flight', '=', $this->flightTable.'.id');
- })
- ->leftJoin($this->airlineTable, function($join) {
- $join->on( $this->flightTable.'.airline', '=', $this->airlineTable.'.id');
- })
- ->latest('bookingId')->get();
- //dd($data);
- return Datatables::of($data)
- ->addIndexColumn()
- ->addColumn('action', function($row){
- //dd($row);
- $view = route($this->route . '.show', Hashids::encode($row->bookingId));
- // dd($view);
- $btn = '<a target="_blank" href="'.$view . '" class="edit btn btn-primary btn-sm">View</a>';
- return $btn;
- })
- ->rawColumns(['action'])
- ->make(true);
- }
- $pageData = [];
- $pageData['pageTitle'] = 'Bookings';
- $pageData['createButtonText'] = 'Create Booking';
- $pageData['listButtonText'] = "Flight List";
- $pageData['route'] = $this->route;
- return View($this->listPage, $pageData);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- $pageData = [];
- $pageData['pageTitle'] = 'Create Booking';
- $pageData['createButtonText'] = 'Create Booking';
- $pageData['listButtonText'] = "Booking List";
- $pageData['route'] = $this->route;
- $pageData['customers'] = $this->customerModel::select(
- [
- 'id',
- DB::Raw("CONCAT(title,'. ',first_name,' ',last_name) as name")
- ]
- )->latest('id')->get();
- // dd($pageData['customers']);
- $pageData['airlines'] = $this->airlineModel::select(['id','name'])->latest('id')->get();
- $pageData['airports'] = $this->airportModel::select(['id','name','city_abbr'])->latest('id')->get();
- $pageData['fareFamilies'] = $this->fareFamilyModel::select(['id','name'])->orderBy('id','ASC')->get();
- $pageData['flightTypes'] = $this->flightTypeModel::select(['id','type_name','locality'])->orderBy('id','ASC')->get();
- // dd( $pageData['flightTypes']);
- $pageData['flights'] = $this->flightModel::select(
- [
- $this->flightTable.'.id',
- // $this->airportTable.'.id as porid',
- // $this->airlineTable.'.id as lineId',
- $this->flightTable.'.flight_date',
- // $this->flightTable.'.title',
- $this->airlineTable.'.name as airlineName',
- // $this->table.'.first_price',
- // $this->table.'.business_price',
- // $this->table.'.economy_price',
- $this->flightTable.'.flight_from',
- $this->flightTable.'.flight_to',
- DB::Raw('CASE
- WHEN is_cancelled = 0 THEN \'No\'
- WHEN is_cancelled = 1 THEN \'Yes\'
- ELSE \'Error\'
- END AS Cancel'),
- DB::Raw('CASE
- WHEN is_active = 0 THEN \'Inactive\'
- WHEN is_active = 1 THEN \'Active\'
- ELSE \'Error\'
- END AS Status'),
- // $this->table.'.is_cancelled',
- $this->flightTable.'.take_of_time',
- $this->flightTable.'.landing_time',
- // $this->table.'.is_active',
- DB::Raw("(select `{$this->airportTable}`.`name` from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_from`) as fromName"),
- DB::Raw("(select `{$this->airportTable}`.`name` from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_to`) as toName"),
- ]
- )
- ->leftJoin($this->airlineTable, function($join) {
- $join->on($this->flightTable.'.airline', '=', $this->airlineTable.'.id');
- })
- ->latest('id')->get();
- //$time = date("g:i a", strtotime("{$pageData['flights'][4]->take_of_time} UTC"));
- //dd($pageData['flights']);
- //dd($pageData['airlines'] );
- //dd($pageData['fairFamilies'] );
- return View($this->createPage, $pageData);
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- // dd($request->all());
- $request->validate([
- 'flight' => 'required',
- 'customer' => 'required',
- 'booking_date' => 'required',
- 'fare_family' => 'required',
- 'fare_price' => 'required',
- 'refference_id' => 'required',
- 'ticket_number' => 'required',
- 'apnr' => 'required',
- 'flight_type' => 'required',
- 'ticket_status' => 'required',
- 'base_fare' => 'required',
- 'taxes' => 'required',
- //'other' => 'required',
- // 'service_fee' => 'required',
- 'departing_date' => 'required',
- //'returning_date' => 'required',
- ]);
- $request->request->add(
- [
- 'created_at' => date("Y-m-d H:I:s"),
- 'created_by' => Auth::id(),
- ]);
- $save = $this->model::create(collect($request->all())->forget('_token','customer2')->toArray());
- /******* Make Transaction *******/
- $baseFair = 0;
- $tax = 0;
- $serviceFeee = 0;
- $other = 0;
- /********* Check Fields Empty or Not *********/
- if($request->filled('base_fare')) {
- $baseFair = $request->input('base_fare');
- }
- if($request->filled('taxes')) {
- $tax = $request->input('taxes');
- }
- if($request->filled('other')) {
- $serviceFeee = $request->input('other');
- }
- if($request->filled('service_fee')) {
- $other = $request->input('service_fee');
- }
- $totalAmount = $baseFair + $tax + $serviceFeee + $other;
- $trData = new Transaction();
- $trData->tr_customer = $request->input('customer');
- $trData->tr_type = 'debit';
- $trData->tr_amount = -1 * $totalAmount;
- $trData->tr_note = "Flight Booking #{$save->id}";
- $trData->booking_id = $save->id;
- $trData->created_at = date("Y-m-d H:I:s");
- $trData->created_by = Auth::id();
- $trData->is_active = 1;
- $trData->save();
- /***************** if Payment is not empty ********************/
- $payment = 0;
- if($request->filled('payment')) {
- $payment = $request->input('payment');
- }
- if($payment > 0){
- $trData = new Transaction();
- $trData->tr_customer = $request->input('customer');
- $trData->tr_type = 'credit';
- $trData->tr_amount = $payment;
- $trData->tr_note = "Flight Booking #{$save->id}";
- $trData->booking_id = $save->id;
- $trData->created_at = date("Y-m-d H:I:s");
- $trData->created_by = Auth::id();
- $trData->is_active = 1;
- $trData->save();
- }
- /******* Make Transaction Ends *******/
- // dd($save->id);
- return Redirect::to($this->route . '/'.$save->id)
- ->with('success', 'Booking created successfully.');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- $id = Hashids::decode($id)[0];
- $pageData = [];
- $pageData['booking'] = $this->model->select('*')->where('id', $id)->get()->first();
- $pageData['flight'] = $this->flightModel->select('*')->get()->where('id',$pageData['booking']->flight)->first();
- $pageData['customer'] = $this->customerModel->select('*')->get()->where('id',$pageData['booking']->customer)->first();
- $pageData['fare_family'] = $this->fareFamilyModel->select('*')->get()->where('id',$pageData['booking']->fairFamily)->first();
- $pageData['flight_type'] = $this->flightTypeModel->select('*')->get()->where('id',$pageData['booking']->flight_type)->first();
- $pageData['airline'] = $this->airlineModel->select('*')->get()->where('id',$pageData['flight']->airline)->first();
- $pageData['flightFrom'] = $this->airportModel->select('*')->get()->where('id',$pageData['flight']->flight_from)->first();
- $pageData['flightTo'] = $this->airportModel->select('*')->get()->where('id',$pageData['flight']->flight_to)->first();
- //dd($pageData['booking']);
- //dd($pageData['flight']);
- //dd($pageData['customer']);
- $pageData['pageTitle'] = 'Ticket Info';
- $pageData['createButtonText'] = 'Create Booking';
- $pageData['listButtonText'] = "Flight List";
- $pageData['route'] = $this->route;
- return View($this->showPage, $pageData);
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- //
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- //
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- //
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement