Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void canSubmitBooking(String userId, String hotelId, String fullName, String checkInDate, String checkOutDate,
- int adults, int children, String contactNumber, String email, String roomPreference,
- String yourGcashName, String yourGcashNumber, String referenceNumber) {
- DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
- DatabaseReference userBookingsRef = databaseReference.child("booking_info");
- userBookingsRef.orderByChild("userId").equalTo(userId).addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- boolean canSubmit = true;
- for (DataSnapshot bookingSnapshot : dataSnapshot.getChildren()) {
- String existingCheckInDate = bookingSnapshot.child("checkInDate").getValue(String.class);
- String existingCheckOutDate = bookingSnapshot.child("checkOutDate").getValue(String.class);
- SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
- Date currentDate = new Date();
- Date existingCheckIn = null;
- Date existingCheckOut = null;
- try {
- existingCheckIn = sdf.parse(existingCheckInDate);
- existingCheckOut = sdf.parse(existingCheckOutDate);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- if (existingCheckIn != null && existingCheckOut != null) {
- // Check if the new booking completely falls outside the range of existing booking
- if (!(existingCheckOut.before(existingCheckIn) && existingCheckIn.before(existingCheckOut) ||
- existingCheckIn.after(existingCheckOut) && existingCheckIn.after(existingCheckIn))) {
- canSubmit = false;
- break;
- }
- }
- if (existingCheckOut != null && currentDate.before(existingCheckOut)) {
- canSubmit = false;
- break;
- }
- }
- if (canSubmit) {
- saveBooking(userId, hotelId, fullName, checkInDate, checkOutDate, adults, children, contactNumber, email, roomPreference, yourGcashName, yourGcashNumber, referenceNumber, timestamp);
- } else {
- Toast.makeText(BookForm.this, "You cannot submit a new booking until your current booking has ended.", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Log.e("BookForm", "Database error: " + databaseError.getMessage());
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement