Advertisement
touhid_xml

Laravel Datatable 2

Apr 13th, 2021
2,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Models\Booking;
  6. use App\Models\Transaction;
  7. use Illuminate\Http\Request;
  8. use App\Utility;
  9. use App\Models\Flight;
  10. use App\Models\Airport;
  11. use App\Models\Airline;
  12. use App\Models\Customer;
  13. use App\Models\FareFamily;
  14. use App\Models\FlightType;
  15. use DataTables;
  16. use DB;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Redirect;
  19. use Hashids;
  20. use App\Classes\SmsApi;
  21.  
  22. class BookingController extends Controller
  23. {
  24.     protected $utility;
  25.     protected $model;
  26.     protected $transactionModel;
  27.     protected $airlineModel;
  28.     protected $airportModel;
  29.     protected $flightModel;
  30.     protected $customerModel;
  31.     protected $fareFamilyModel;
  32.     protected $flightTypeModel;
  33.     protected $table;
  34.     protected $flightTable;
  35.     protected $airlineTable;
  36.     protected $airportTable;
  37.     protected $customerTable;
  38.     protected $fareFamilyTable;
  39.     protected $flightTypeTable;
  40.     protected $transactionTable;
  41.     protected $route;
  42.     protected $createPage;
  43.     protected $listPage;
  44.     protected $showPage;
  45.  
  46.     protected $smsApi;
  47.     public function __construct()
  48.     {
  49.         $this->model            =       new Booking();
  50.         $this->flightModel      =       new Flight();
  51.         $this->transactionModel =       new Transaction();
  52.         $this->airlineModel     =       new Airline();
  53.         $this->airportModel     =       new Airport();
  54.         $this->customerModel    =       new Customer();
  55.         $this->fareFamilyModel  =       new FareFamily();
  56.         $this->flightTypeModel  =       new FlightType();
  57.         $this->table            =       $this->model->getTable();
  58.         $this->transactionTable =       $this->transactionModel->getTable();
  59.         $this->airlineTable     =       $this->airlineModel->getTable();
  60.         $this->airportTable     =       $this->airportModel->getTable();
  61.         $this->flightTable      =       $this->flightModel->getTable();
  62.         $this->customerTable    =       $this->customerModel->getTable();
  63.         $this->fareFamilyTable  =       $this->fareFamilyModel->getTable();
  64.         $this->flightTypeTable  =       $this->flightTypeModel->getTable();
  65.         $this->route            =       Utility::bookingRoute();
  66.         $this->createPage       =       'pages.booking_create';
  67.         $this->listPage         =       'pages.booking_list';
  68.         $this->showPage         =       'pages.booking_show';
  69.         $this->smsApi           =       new SmsApi();
  70.     }
  71.  
  72.     /**
  73.      * Display a listing of the resource.
  74.      *
  75.      * @return \Illuminate\Http\Response
  76.      */
  77.     public function index(Request $request)
  78.     {
  79.        // dd($this->smsApi->sendMessage('01858023977','Api testing message',) );
  80.         //dd($request->input('draw'));
  81.         if ($request->ajax() || $request->input('draw') == 1) {
  82.             $data = $this->model::select(
  83.  
  84.                 //Booking Info
  85.                 [$this->table . '.id as bookingId',
  86.                 $this->table . '.flight',
  87.                 $this->table . '.customer',
  88.                 $this->table . '.booking_date',
  89. //                $this->table . '.cabin_class',
  90.                 $this->table . '.fare_family',
  91.                 $this->table . '.fare_price',
  92.                 $this->table . '.is_cancelled',
  93.                 $this->table . '.refund_reason',
  94.                 $this->table . '.status as bookingStatus',
  95.  
  96.                //Customer Info
  97.                $this->customerTable.'.id as customerId',
  98.                $this->customerTable.'.title',
  99.                $this->customerTable.'.first_name',
  100.                $this->customerTable.'.last_name',
  101.                $this->customerTable.'.dob',
  102.                $this->customerTable.'.email',
  103.                $this->customerTable.'.phone1',
  104.                $this->customerTable.'.phone2',
  105.                $this->customerTable.'.phone3',
  106.                DB::Raw("CONCAT({$this->customerTable}.title,'. ', {$this->customerTable}.first_name, ' ' , {$this->customerTable}.last_name) as CustomerFullName"),
  107.  
  108.               //Fair Family Info
  109.               $this->fareFamilyTable.'.name as fairFamilyName',
  110.  
  111.               // Flight Info
  112. //              $this->flightTable.'.title as FlightTitle',
  113.               $this->flightTable.'.airline',
  114.               $this->flightTable.'.fare_type',
  115.               $this->flightTable.'.flight_from',
  116.               $this->flightTable.'.flight_to',
  117.               DB::Raw("(select `{$this->airportTable}`.`name`  from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_from`) as fromName"),
  118.               DB::Raw("(select `{$this->airportTable}`.`name`  from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_to`) as toName"),
  119.               $this->flightTable.'.take_of_time',
  120.               $this->flightTable.'.landing_time',
  121.               $this->flightTable.'.flight_date',
  122.  
  123.               //Airline Info
  124.               $this->airlineTable.'.name as AirlineName'
  125.                 ]
  126.             )
  127.  
  128.                 ->leftJoin($this->customerTable, function($join) {
  129.                     $join->on($this->table.'.customer', '=', $this->customerTable.'.id');
  130.                 })
  131.  
  132.                 ->leftJoin($this->fareFamilyTable, function($join) {
  133.                     $join->on($this->table.'.fare_family', '=', $this->fareFamilyTable.'.id');
  134.                 })
  135.  
  136.                 ->leftJoin($this->flightTable, function($join) {
  137.                     $join->on($this->table.'.flight', '=', $this->flightTable.'.id');
  138.                 })
  139.  
  140.                 ->leftJoin($this->airlineTable, function($join) {
  141.                     $join->on( $this->flightTable.'.airline', '=', $this->airlineTable.'.id');
  142.                 })
  143.  
  144.  
  145.  
  146.                 ->latest('bookingId')->get();
  147.             //dd($data);
  148.             return Datatables::of($data)
  149.                 ->addIndexColumn()
  150.                 ->addColumn('action', function($row){
  151.                     //dd($row);
  152.                     $view = route($this->route . '.show', Hashids::encode($row->bookingId));
  153.                    // dd($view);
  154.                     $btn = '<a target="_blank" href="'.$view . '" class="edit btn btn-primary btn-sm">View</a>';
  155.                     return $btn;
  156.                 })
  157.                 ->rawColumns(['action'])
  158.                 ->make(true);
  159.         }
  160.  
  161.  
  162.         $pageData = [];
  163.         $pageData['pageTitle'] = 'Bookings';
  164.         $pageData['createButtonText'] = 'Create Booking';
  165.         $pageData['listButtonText'] = "Flight List";
  166.         $pageData['route'] = $this->route;
  167.         return View($this->listPage, $pageData);
  168.     }
  169.  
  170.     /**
  171.      * Show the form for creating a new resource.
  172.      *
  173.      * @return \Illuminate\Http\Response
  174.      */
  175.     public function create()
  176.     {
  177.         $pageData = [];
  178.         $pageData['pageTitle'] = 'Create Booking';
  179.         $pageData['createButtonText'] = 'Create Booking';
  180.         $pageData['listButtonText'] = "Booking List";
  181.         $pageData['route'] = $this->route;
  182.  
  183.         $pageData['customers'] = $this->customerModel::select(
  184.             [
  185.                 'id',
  186.                 DB::Raw("CONCAT(title,'. ',first_name,' ',last_name) as name")
  187.             ]
  188.         )->latest('id')->get();
  189. //            dd($pageData['customers']);
  190.         $pageData['airlines'] = $this->airlineModel::select(['id','name'])->latest('id')->get();
  191.         $pageData['airports'] = $this->airportModel::select(['id','name','city_abbr'])->latest('id')->get();
  192.         $pageData['fareFamilies'] = $this->fareFamilyModel::select(['id','name'])->orderBy('id','ASC')->get();
  193.         $pageData['flightTypes'] = $this->flightTypeModel::select(['id','type_name','locality'])->orderBy('id','ASC')->get();
  194.        // dd( $pageData['flightTypes']);
  195.         $pageData['flights'] = $this->flightModel::select(
  196.             [
  197.  
  198.                 $this->flightTable.'.id',
  199. //                    $this->airportTable.'.id as porid',
  200. //                    $this->airlineTable.'.id as lineId',
  201.  
  202.  
  203.  
  204.                 $this->flightTable.'.flight_date',
  205. //                $this->flightTable.'.title',
  206.                 $this->airlineTable.'.name as airlineName',
  207.                 //  $this->table.'.first_price',
  208.                 //  $this->table.'.business_price',
  209.                 // $this->table.'.economy_price',
  210.                 $this->flightTable.'.flight_from',
  211.                 $this->flightTable.'.flight_to',
  212.                 DB::Raw('CASE
  213.                    WHEN is_cancelled = 0 THEN \'No\'
  214.                    WHEN is_cancelled = 1 THEN \'Yes\'
  215.                    ELSE \'Error\'
  216.                    END AS Cancel'),
  217.                 DB::Raw('CASE
  218.                    WHEN is_active = 0 THEN \'Inactive\'
  219.                    WHEN is_active = 1 THEN \'Active\'
  220.                    ELSE \'Error\'
  221.                    END AS Status'),
  222.                 // $this->table.'.is_cancelled',
  223.                 $this->flightTable.'.take_of_time',
  224.                 $this->flightTable.'.landing_time',
  225.                 //  $this->table.'.is_active',
  226.                 DB::Raw("(select `{$this->airportTable}`.`name`  from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_from`) as fromName"),
  227.                 DB::Raw("(select `{$this->airportTable}`.`name`  from `{$this->airportTable}` WHERE `{$this->airportTable}`.`id` = `{$this->flightTable}`.`flight_to`) as toName"),
  228.  
  229.             ]
  230.         )
  231.             ->leftJoin($this->airlineTable, function($join) {
  232.                 $join->on($this->flightTable.'.airline', '=', $this->airlineTable.'.id');
  233.             })
  234.  
  235.             ->latest('id')->get();
  236.         //$time =  date("g:i a", strtotime("{$pageData['flights'][4]->take_of_time} UTC"));
  237.        //dd($pageData['flights']);
  238.         //dd($pageData['airlines'] );
  239.         //dd($pageData['fairFamilies'] );
  240.         return View($this->createPage, $pageData);
  241.     }
  242.  
  243.     /**
  244.      * Store a newly created resource in storage.
  245.      *
  246.      * @param  \Illuminate\Http\Request  $request
  247.      * @return \Illuminate\Http\Response
  248.      */
  249.     public function store(Request $request)
  250.     {
  251. //        dd($request->all());
  252.  
  253.         $request->validate([
  254.             'flight' => 'required',
  255.             'customer' => 'required',
  256.             'booking_date' => 'required',
  257.             'fare_family' => 'required',
  258.             'fare_price' => 'required',
  259.             'refference_id' => 'required',
  260.             'ticket_number' => 'required',
  261.             'apnr' => 'required',
  262.             'flight_type' => 'required',
  263.             'ticket_status' => 'required',
  264.             'base_fare' => 'required',
  265.             'taxes' => 'required',
  266.             //'other' => 'required',
  267.            // 'service_fee' => 'required',
  268.             'departing_date' => 'required',
  269.             //'returning_date' => 'required',
  270.  
  271.  
  272.         ]);
  273.         $request->request->add(
  274.             [
  275.                 'created_at' => date("Y-m-d H:I:s"),
  276.                 'created_by' => Auth::id(),
  277.             ]);
  278.  
  279.  
  280.  
  281.       $save =   $this->model::create(collect($request->all())->forget('_token','customer2')->toArray());
  282.  
  283.       /******* Make Transaction *******/
  284.  
  285.         $baseFair       =   0;
  286.         $tax            =   0;
  287.         $serviceFeee    =   0;
  288.         $other          =   0;
  289.  
  290.         /********* Check Fields Empty or Not *********/
  291.         if($request->filled('base_fare')) {
  292.             $baseFair       = $request->input('base_fare');
  293.         }
  294.         if($request->filled('taxes')) {
  295.             $tax            = $request->input('taxes');
  296.         }
  297.         if($request->filled('other')) {
  298.             $serviceFeee    = $request->input('other');
  299.         }
  300.         if($request->filled('service_fee')) {
  301.             $other          = $request->input('service_fee');
  302.         }
  303.  
  304.         $totalAmount = $baseFair + $tax + $serviceFeee + $other;
  305.  
  306.  
  307.  
  308.         $trData = new Transaction();
  309.         $trData->tr_customer = $request->input('customer');
  310.         $trData->tr_type = 'debit';
  311.         $trData->tr_amount = -1 * $totalAmount;
  312.         $trData->tr_note = "Flight Booking #{$save->id}";
  313.         $trData->booking_id = $save->id;
  314.         $trData->created_at =  date("Y-m-d H:I:s");
  315.         $trData->created_by = Auth::id();
  316.         $trData->is_active = 1;
  317.         $trData->save();
  318.  
  319.         /***************** if Payment is not empty ********************/
  320.         $payment = 0;
  321.         if($request->filled('payment')) {
  322.             $payment = $request->input('payment');
  323.         }
  324.         if($payment > 0){
  325.             $trData = new Transaction();
  326.             $trData->tr_customer = $request->input('customer');
  327.             $trData->tr_type = 'credit';
  328.             $trData->tr_amount = $payment;
  329.             $trData->tr_note = "Flight Booking #{$save->id}";
  330.             $trData->booking_id = $save->id;
  331.             $trData->created_at =  date("Y-m-d H:I:s");
  332.             $trData->created_by = Auth::id();
  333.             $trData->is_active = 1;
  334.             $trData->save();
  335.         }
  336.  
  337.  
  338.         /******* Make Transaction Ends *******/
  339.  
  340.      // dd($save->id);
  341.  
  342.         return Redirect::to($this->route . '/'.$save->id)
  343.             ->with('success', 'Booking created successfully.');
  344.     }
  345.  
  346.     /**
  347.      * Display the specified resource.
  348.      *
  349.      * @param  int  $id
  350.      * @return \Illuminate\Http\Response
  351.      */
  352.     public function show($id)
  353.     {
  354.         $id = Hashids::decode($id)[0];
  355.         $pageData = [];
  356.         $pageData['booking'] = $this->model->select('*')->where('id', $id)->get()->first();
  357.         $pageData['flight'] = $this->flightModel->select('*')->get()->where('id',$pageData['booking']->flight)->first();
  358.         $pageData['customer'] = $this->customerModel->select('*')->get()->where('id',$pageData['booking']->customer)->first();
  359.         $pageData['fare_family'] = $this->fareFamilyModel->select('*')->get()->where('id',$pageData['booking']->fairFamily)->first();
  360.         $pageData['flight_type'] = $this->flightTypeModel->select('*')->get()->where('id',$pageData['booking']->flight_type)->first();
  361.         $pageData['airline'] = $this->airlineModel->select('*')->get()->where('id',$pageData['flight']->airline)->first();
  362.         $pageData['flightFrom'] = $this->airportModel->select('*')->get()->where('id',$pageData['flight']->flight_from)->first();
  363.         $pageData['flightTo'] = $this->airportModel->select('*')->get()->where('id',$pageData['flight']->flight_to)->first();
  364.  
  365.  
  366.  
  367.         //dd($pageData['booking']);
  368.         //dd($pageData['flight']);
  369.         //dd($pageData['customer']);
  370.         $pageData['pageTitle'] = 'Ticket Info';
  371.         $pageData['createButtonText'] = 'Create Booking';
  372.         $pageData['listButtonText'] = "Flight List";
  373.         $pageData['route'] = $this->route;
  374.         return View($this->showPage, $pageData);
  375.     }
  376.  
  377.     /**
  378.      * Show the form for editing the specified resource.
  379.      *
  380.      * @param  int  $id
  381.      * @return \Illuminate\Http\Response
  382.      */
  383.     public function edit($id)
  384.     {
  385.         //
  386.     }
  387.  
  388.     /**
  389.      * Update the specified resource in storage.
  390.      *
  391.      * @param  \Illuminate\Http\Request  $request
  392.      * @param  int  $id
  393.      * @return \Illuminate\Http\Response
  394.      */
  395.     public function update(Request $request, $id)
  396.     {
  397.         //
  398.     }
  399.  
  400.     /**
  401.      * Remove the specified resource from storage.
  402.      *
  403.      * @param  int  $id
  404.      * @return \Illuminate\Http\Response
  405.      */
  406.     public function destroy($id)
  407.     {
  408.         //
  409.     }
  410. }
  411.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement