/** * BOOKING */ public function add_booking() { global $wpdb; try { $edit = false; if (isset($_POST['id_booking']) && !empty($_POST['id_booking'])) { $edit = true; $id_booking = (int) $_POST['id_booking']; if ($id_booking <= 0) { throw new Exception("Invalid Booking ID"); } } $enter_date = Date('Y-m-d H:i:s', strtotime($_POST['enter_date'] . ' ' . $_POST['enter_date_hour'])); $return_date = Date('Y-m-d H:i:s', strtotime($_POST['return_date'] . ' ' . $_POST['return_date_hour'])); $branches = self::get_branches(); $enter_loc = self::get_branch($_POST['id_enter_branch'], $branches) ? self::get_branch($_POST['id_enter_branch'], $branches)->name : ''; $return_loc = self::get_branch($_POST['id_return_branch'], $branches) ? self::get_branch($_POST['id_return_branch'], $branches)->name : ''; // Save booking to DB $arr = array('first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name'], 'email' => $_POST['email'], 'phone' => $_POST['phone'], 'street' => $_POST['street'], 'city' => $_POST['city'], 'zip' => $_POST['zip'], 'country' => $_POST['country'], 'company' => $_POST['company'], 'vat' => $_POST['vat'], 'flight' => $_POST['flight'], 'license' => $_POST['license'], 'id_card' => $_POST['id_card'], 'enter_loc' => $enter_loc, 'id_enter_branch' => $_POST['id_enter_branch'], 'enter_date' => $enter_date, 'return_loc' => $return_loc, 'id_return_branch' => $_POST['id_return_branch'], 'return_date' => $return_date, 'payment_option' => $_POST['payment_option'], 'comment' => $_POST['comment'], 'partner_code' => $_POST['partner_code'], 'status' => (int) $_POST['status'], 'paid_online' => (double) $_POST['paid_online']); if ((int) $_POST['change_vehicle'] > 0) { $vehicle = CarRental::get_vehicle_parameters((int) $_POST['change_vehicle']); $vehicle->consumption_metric = get_option('carrental_consumption'); $currency = get_option('carrental_global_currency'); $distance_metric = get_option('carrental_distance_metric'); $vehicle_arr = array('vehicle' => $vehicle->name, 'vehicle_id' => $vehicle->id_fleet, 'vehicle_picture' => $vehicle->picture, 'vehicle_ac' => $vehicle->ac, 'vehicle_luggage' => $vehicle->luggage, 'vehicle_seats' => $vehicle->seats, 'vehicle_fuel' => $vehicle->fuel, 'vehicle_consumption' => $vehicle->consumption, 'vehicle_consumption_metric' => $vehicle->consumption_metric, 'vehicle_transmission' => $vehicle->transmission, 'vehicle_free_distance' => $vehicle->free_distance . ' ' . $distance_metric, 'vehicle_deposit' => $vehicle->deposit . ' ' . $currency); $arr = array_merge($arr, $vehicle_arr); } if ($edit == true) { // Update booking $arr['updated'] = Date('Y-m-d H:i:s'); $wpdb->update(CarRental::$db['booking'], $arr, array('id_booking' => $id_booking)); // Delete drivers $wpdb->delete(CarRental::$db['booking_drivers'], array('id_booking' => $id_booking), array('%d')); // Delete prices $wpdb->delete(CarRental::$db['booking_prices'], array('id_booking' => $id_booking), array('%d')); } else { // Add booking $id_order = CarRental::generate_unique_order_id(); $arr['id_order'] = $id_order; $arr['terms'] = 1; $wpdb->insert(CarRental::$db['booking'], $arr); $id_booking = $wpdb->insert_id; } if (isset($_POST['add_booking_emails'])) { switch ($_POST['status']) { case 1: // confirmed self::resend_email($id_booking, 'carrental_reservation_email'); break; case 2: // pending payment self::resend_email($id_booking, 'carrental_email_status_pending'); break; case 3: // panding other self::resend_email($id_booking, 'carrental_email_status_pending_other'); break; } } // Add drivers if ($_POST['drv'] && !empty($_POST['drv'])) { foreach ($_POST['drv']['email'] as $key => $val) { if (!empty($val) && !empty($_POST['drv']['first_name'][$key]) && !empty($_POST['drv']['last_name'][$key]) && !empty($_POST['drv']['phone'][$key])) { $arr = array('id_booking' => $id_booking, 'first_name' => $_POST['drv']['first_name'][$key], 'last_name' => $_POST['drv']['last_name'][$key], 'email' => $val, 'phone' => $_POST['drv']['phone'][$key], 'street' => $_POST['drv']['street'][$key], 'city' => $_POST['drv']['city'][$key], 'zip' => $_POST['drv']['zip'][$key], 'country' => $_POST['drv']['country'][$key], 'license' => $_POST['drv']['license'][$key], 'id_card' => $_POST['drv']['id_card'][$key]); $wpdb->insert(CarRental::$db['booking_drivers'], $arr); } } } // Add prices if ($_POST['prices'] && !empty($_POST['prices'])) { foreach ($_POST['prices']['name'] as $key => $val) { if (!empty($val) && !empty($_POST['prices']['price'][$key]) && !empty($_POST['prices']['currency'][$key])) { $arr = array('id_booking' => $id_booking, 'name' => $val, 'price' => $_POST['prices']['price'][$key], 'currency' => $_POST['prices']['currency'][$key]); $wpdb->insert(CarRental::$db['booking_prices'], $arr); } } } do_action('carrental_admin_after_booking_save', $id_booking); return true; } catch (Exception $e) { return $e->getMessage(); } }