public function update($id)
 {
     $today = date("Y-m-d H:i:s");
     $i = Input::all();
     $bookingRemarks = '';
     $addition = 0;
     $deduction = 0;
     if ($i['savethis'] == true) {
         $addition -= $i['price_addition'];
         $deduction += $i['price_deduction'];
     }
     $booking = Booking::where('id', $id)->with('reservedRoom.room.roomDetails')->first();
     //$booking = ReservedRoom::where('id', $id)->with('room.roomDetails')->first();
     $old_status = $booking->status;
     $current_price = $booking->price;
     $current_price += $addition + $deduction;
     if (!empty($booking)) {
         $booking->status = $i['status'];
         if ($i['status'] == 5) {
             $booking->cancelled_at = $today;
             $booking->paid = 0;
             $booking->price = 0;
             $booking->cancelled_remarks = $i['cancelled_remarks'];
         } else {
             if ($i['status'] == 1) {
                 $booking->paid = $i['paid'];
             } else {
                 //$booking->paid=0;
                 $booking->price = $current_price;
                 $booking->cancelled_remarks = '';
                 $booking->cancelled_at = '0000-00-00 00:00:00';
             }
         }
         if ($booking->save()) {
             if ($i['savethis'] == true) {
                 $newBookingRemarks = new BookingRemarksHistory();
                 $newBookingRemarks->additional = $addition;
                 $newBookingRemarks->deduction = $deduction;
                 $newBookingRemarks->remarks = $i['bookingremarks'];
                 $newBookingRemarks->booking_id = $id;
                 $newBookingRemarks->save();
             }
             $roomReserved = ReservedRoom::where('booking_id', $booking->id)->get();
             foreach ($roomReserved as $rr) {
                 $rr->status = $i['status'];
                 $rr->save();
             }
             $a = new Activity();
             $a->actor = Auth::id();
             $a->location = 3;
             $a->logs = 'Updated booking ID of:' . $booking->id . ' by ' . $booking->firstname . ' ' . $booking->lastname . ' from ' . $this->bookingStatus($old_status) . ' to ' . $this->bookingStatus($booking->status);
             //before to after status.
             $a->save();
             return $booking;
         } else {
             return '0';
             //means failed
         }
     }
 }
 public function getPaymentStatus()
 {
     // Get the payment ID before session clear
     $payment_id = Session::get('paypal_payment_id');
     // clear the session payment ID
     Session::forget('paypal_payment_id');
     if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
         return Redirect::route('original.route')->with('error', 'Payment failed');
     }
     $payment = Payment::get($payment_id, $this->_api_context);
     // PaymentExecution object includes information necessary
     // to execute a PayPal account payment.
     // The payer_id is added to the request query parameters
     // when the user is redirected from paypal back to your site
     $execution = new PaymentExecution();
     $execution->setPayerId(Input::get('PayerID'));
     //Execute the payment
     $result = $payment->execute($execution, $this->_api_context);
     /*echo '<pre>';print_r($result);echo '</pre>';exit; // DEBUG RESULT, remove it later*/
     if ($result->getState() == 'approved') {
         // payment made
         // $tax = null;
         $total_price = null;
         $i = [];
         $i['checkin'] = Session::get('reservation')['checkin'] . '12:00:00';
         $i['checkout'] = Session::get('reservation')['checkout'] . '11:59:00';
         $customerinformation = Session::get('reservation.customerinformation');
         $count = 0;
         //for test
         $count1 = 0;
         //for test
         $booked_room = [];
         //all picked rooms from available rooms
         $new_booking = new Booking();
         $new_booking->firstname = $customerinformation['firstname'];
         $new_booking->lastname = $customerinformation['lastname'];
         $new_booking->address = $customerinformation['address'];
         $new_booking->contact_number = $customerinformation['contact_no'];
         $new_booking->email_address = $customerinformation['email'];
         $new_booking->check_in = $i['checkin'];
         $new_booking->check_out = $i['checkout'];
         $new_booking->payment_type = 'paypal';
         $new_booking->paypal_paymentId = Input::get('PayerID');
         $new_booking->save();
         if (Session::has('hasMembershipDiscount')) {
             $booking_remarks = new BookingRemarksHistory();
             $booking_remarks->remarks = "Membership Discount";
             $booking_remarks->deduction = Session::get('hasMembershipDiscount');
             $booking_remarks->booking_id = $new_booking->id;
             $booking_remarks->save();
             Session::forget('hasMembershipDiscount');
         }
         if (Session::has('hasCouponDiscount')) {
             $booking_remarks = new BookingRemarksHistory();
             $booking_remarks->remarks = "Coupon Discount";
             $booking_remarks->deduction = Session::get('hasCouponDiscount');
             $booking_remarks->booking_id = $new_booking->id;
             $booking_remarks->save();
             Session::forget('hasCouponDiscount');
         }
         foreach (Session::get('reservation')['reservation_room'] as $rooms) {
             $count++;
             $room_id = $rooms['room_details']['id'];
             $available_rooms = [];
             $room_qty = RoomQty::with(array('roomPrice', 'roomReserved' => function ($query) use($i, $room_id) {
                 $query->where(function ($query2) use($i, $room_id) {
                     $query2->whereBetween('check_in', array($i['checkin'], $i['checkout']))->orWhereBetween('check_out', array($i['checkin'], $i['checkout']))->orWhereRaw('"' . $i["checkin"] . '" between check_in and check_out')->orWhereRaw('"' . $i["checkout"] . '" between check_in and check_out');
                 })->where(function ($query3) {
                     $query3->where('status', '!=', 5)->orWhere('status', '!=', 3);
                 });
             }))->where('room_id', $room_id)->where('status', 1)->get();
             foreach ($room_qty as $available) {
                 if ($available->roomReserved == '[]') {
                     array_push($available_rooms, $available);
                 }
             }
             for ($counter = 0; $counter < $rooms['quantity']; $counter++) {
                 array_push($booked_room, $available_rooms[$counter]);
             }
         }
         $total = 0;
         if (!empty($booked_room)) {
             foreach ($booked_room as $b) {
                 $roomprice = $b->roomPrice->price * Session::get('reservation.nights');
                 $total += $b->roomPrice->price * Session::get('reservation.nights');
                 //$roomprice+=$tax;
                 //$total = $total + $tax;
                 $reserveRoom = new ReservedRoom();
                 $reserveRoom->booking_id = $new_booking->id;
                 $reserveRoom->room_id = $b->id;
                 $reserveRoom->room_type = $b->room_id;
                 $reserveRoom->price = $roomprice;
                 $reserveRoom->check_in = $i['checkin'];
                 $reserveRoom->check_out = $i['checkout'];
                 $reserveRoom->status = 1;
                 $reserveRoom->firstname = $customerinformation['firstname'];
                 $reserveRoom->lastname = $customerinformation['lastname'];
                 $reserveRoom->address = $customerinformation['address'];
                 $reserveRoom->contact_number = $customerinformation['contact_no'];
                 $reserveRoom->email_address = $customerinformation['email'];
                 $reserveRoom->save();
             }
         }
         //$tax = $total * 0.12;
         //$total = $total + $tax;
         $new_booking->price = Session::get('total_price');
         $new_booking->paid = Session::get('total_price');
         $new_booking->status = 1;
         $date = date('Ymd');
         $code = strtolower(Str::random(5) . $date);
         $new_booking->code = $code;
         $new_booking->save();
         Session::forget('reservation');
         Session::forget('total_price');
         return Redirect::to('booking/step5')->with('code', $code);
     }
     return 'error in payment';
 }
     $membershipDiscount = null;
     if (isset($i['membership_id'])) {
         $membership = Customer::where('membership_id', $i['membership_id'])->first();
         if ($membership) {
             $membershipDiscount = $membership->current_discount;
         }
     }
     $booking = Booking::where('id', $i['booking_id'])->first();
     if ($booking) {
         $booking->firstname = $i['firstname'];
         $booking->lastname = $i['lastname'];
         $booking->address = $i['address'];
         $booking->contact_number = $i['contact_no'];
         $booking->status = 1;
         $booking->save();
         $reservation = ReservedRoom::where('booking_id', $i['booking_id'])->get();
         if (count($reservation) > 0) {
             foreach ($reservation as $r) {
                 $r->firstname = Input::has('firstname') ? $i['firstname'] : 'WALK-IN';
                 $r->lastname = Input::has('lastname') ? $i['lastname'] : 'WALK-IN';
                 $r->address = Input::has('address') ? $i['address'] : 'WALK-IN';
                 $r->contact_number = Input::has('contact_no') ? $i['contact_no'] : 'WALK-IN';
                 $r->status = 1;
                 if ($r->save()) {
                 }
             }
         }
         return $i;
     }
 });
 Route::get('reports', 'ReportsController@index');
 public function bookingStep2($id)
 {
     $i = Input::all();
     $i['checkin'] = $i['checkin'] . ' 12:00:00';
     //
     $i['checkout'] = $i['checkout'] . ' 11:59:00';
     $total_price = null;
     $tax = null;
     $booking_session = [];
     $count = 0;
     //for test
     $count1 = 0;
     //for test
     $booked_room = [];
     //all picked rooms from available rooms
     $booked_room_id = [];
     //foreach(Session::get('reservation')['reservation_room'] as $rooms){
     $count++;
     $room_id = $id;
     $available_rooms = [];
     $room_qty = RoomQty::with(array('roomPrice', 'roomReserved' => function ($query) use($i, $room_id) {
         $query->where(function ($query2) use($i, $room_id) {
             $query2->whereBetween('check_in', array($i['checkin'], $i['checkout']))->orWhereBetween('check_out', array($i['checkin'], $i['checkout']))->orWhereRaw('"' . $i["checkin"] . '" between check_in and check_out')->orWhereRaw('"' . $i["checkout"] . '" between check_in and check_out');
         })->where(function ($query3) {
             $query3->where('status', '!=', 5)->where('status', '!=', 3);
         });
     }))->where('status', 1)->where('room_id', $room_id)->get();
     foreach ($room_qty as $available) {
         if (count($available->roomReserved) == 0) {
             array_push($available_rooms, $available);
         } else {
         }
     }
     if (!empty($available_rooms)) {
         for ($counter = 0; $counter < $i['quantity']; $counter++) {
             array_push($booked_room, $available_rooms[$counter]);
         }
     } else {
         return '0';
         //not available
     }
     //} //end of foreach
     $total_price = 0;
     if (!isset($_GET['bookingId'])) {
         $new_booking = new Booking();
         $new_booking->firstname = 'WALK-IN';
         $new_booking->lastname = 'WALK-IN';
         $new_booking->address = 'WALK-IN';
         $new_booking->contact_number = '000';
         $new_booking->check_in = $i['checkin'];
         $new_booking->check_out = $i['checkout'];
         $new_booking->email_address = 'WALK-IN';
         $new_booking->code = 'N/A';
         $new_booking->status = 0;
         $new_booking->save();
     }
     foreach ($booked_room as $b) {
         $ci = new Carbon($i['checkin']);
         $co = new Carbon($i['checkout']);
         $total_nights = $co->diff($ci)->days;
         $total_nights += 1;
         $total_price += $total_nights * $b->roomPrice->price;
         $total_price2 = $total_nights * $b->roomPrice->price;
         //$tax = $total_price2 * 0.12;
         //$total_price2+=$tax;
         $reserveRoom = new ReservedRoom();
         $reserveRoom->room_id = $b->id;
         $reserveRoom->room_type = $b->room_id;
         $reserveRoom->booking_id = isset($_GET['bookingId']) ? $_GET['bookingId'] : $new_booking->id;
         $reserveRoom->check_in = $i['checkin'];
         $reserveRoom->check_out = $i['checkout'];
         $reserveRoom->code = 'N/A';
         /*$reserveRoom->firstname = 'WALK-IN';
         		$reserveRoom->lastname = 'WALK-IN';*/
         $reserveRoom->price = $total_price2;
         /*$reserveRoom->address = 'WALK-IN';
         		$reserveRoom->contact_number = '000';
         		$reserveRoom->email_address = 'WALK-IN';*/
         $reserveRoom->status = '0';
         if ($reserveRoom->save()) {
             array_push($booked_room_id, $reserveRoom->id);
         }
     }
     if (isset($_GET['bookingId'])) {
         $existing_booking = Booking::where('id', $_GET['bookingId'])->first();
         if ($existing_booking) {
             $existing_booking->price = $total_price;
         }
     } else {
         $new_booking->price = $total_price;
         $new_booking->save();
     }
     //	return Session::get('reservation');
     //return $booked_room;
     //return $counter;
     //return $room_qty1;
     $booking_session['booking_id'] = isset($_GET['bookingId']) ? $_GET['bookingId'] : $new_booking->id;
     $booking_session['rooms'] = $booked_room_id;
     return $booking_session;
 }