def do(self): try: # Update confirmation_status for b in Booking.objects.all(): if not b.paid and b.confirmation_sent: b.confirmation_sent = False b.save() unconfirmed = Booking.objects.filter(confirmation_sent=False) if unconfirmed: for b in unconfirmed: if b.paid: send_booking_confirmation(b) except: raise
def get(self, request, *args, **kwargs): try: booking = utils.get_session_booking(request.session) invoice_ref = request.GET.get('invoice') if booking.booking_type == 3: try: inv = Invoice.objects.get(reference=invoice_ref) except Invoice.DoesNotExist: logger.error( '{} tried making a booking with an incorrect invoice'. format('User {} with id {}'.format( booking.customer.get_full_name(), booking.customer. id) if booking.customer else 'An anonymous user')) return redirect('public_make_booking') if inv.system not in ['0019']: logger.error( '{} tried making a booking with an invoice from another system with reference number {}' .format( 'User {} with id {}'.format( booking.customer.get_full_name(), booking.customer.id) if booking.customer else 'An anonymous user', inv.reference)) return redirect('public_make_booking') try: b = BookingInvoice.objects.get( invoice_reference=invoice_ref) logger.error( '{} tried making a booking with an already used invoice with reference number {}' .format( 'User {} with id {}'.format( booking.customer.get_full_name(), booking.customer.id) if booking.customer else 'An anonymous user', inv.reference)) return redirect('public_make_booking') except BookingInvoice.DoesNotExist: logger.info( '{} finished temporary booking {}, creating new BookingInvoice with reference {}' .format( 'User {} with id {}'.format( booking.customer.get_full_name(), booking.customer.id) if booking.customer else 'An anonymous user', booking.id, invoice_ref)) # FIXME: replace with server side notify_url callback book_inv, created = BookingInvoice.objects.get_or_create( booking=booking, invoice_reference=invoice_ref) # set booking to be permanent fixture booking.booking_type = 1 # internet booking booking.expiry_time = None booking.save() utils.delete_session_booking(request.session) request.session['ps_last_booking'] = booking.id # send out the invoice before the confirmation is sent emails.send_booking_invoice(booking) # for fully paid bookings, fire off confirmation email if booking.paid: emails.send_booking_confirmation(booking, request) except Exception as e: if 'ps_booking_internal' in request.COOKIES: return redirect('home') elif ('ps_last_booking' in request.session) and Booking.objects.filter( id=request.session['ps_last_booking']).exists(): booking = Booking.objects.get( id=request.session['ps_last_booking']) else: return redirect('home') context = {'booking': booking} return render(request, self.template_name, context)
def update_booking(request, old_booking, booking_details): same_dates = False same_campsites = False same_campground = False same_details = False same_vehicles = True with transaction.atomic(): try: set_session_booking(request.session, old_booking) new_details = {} new_details.update(old_booking.details) # Update the guests new_details['num_adult'] = booking_details['num_adult'] new_details['num_concession'] = booking_details['num_concession'] new_details['num_child'] = booking_details['num_child'] new_details['num_infant'] = booking_details['num_infant'] booking = Booking(arrival=booking_details['start_date'], departure=booking_details['end_date'], details=new_details, customer=old_booking.customer, campground=Campground.objects.get( id=booking_details['campground'])) # Check that the departure is not less than the arrival if booking.departure < booking.arrival: raise Exception( 'The departure date cannot be before the arrival date') today = datetime.now().date() if today > old_booking.departure: raise ValidationError( 'You cannot change a booking past the departure date.') # Check if it is the same campground if old_booking.campground.id == booking.campground.id: same_campground = True # Check if dates are the same if (old_booking.arrival == booking.arrival) and (old_booking.departure == booking.departure): same_dates = True # Check if the campsite is the same if sorted(old_booking.campsite_id_list) == sorted( booking_details['campsites']): same_campsites = True # Check if the details have changed if new_details == old_booking.details: same_details = True # Check if the vehicles have changed current_regos = old_booking.regos.all() current_vehicle_regos = sorted([r.rego for r in current_regos]) # Add history new_history = old_booking._generate_history(user=request.user) if request.data.get('entryFees').get('regos'): new_regos = request.data['entryFees'].pop('regos') sent_regos = [r['rego'] for r in new_regos] regos_serializers = [] update_regos_serializers = [] for n in new_regos: if n['rego'] not in current_vehicle_regos: n['booking'] = old_booking.id regos_serializers.append(BookingRegoSerializer(data=n)) same_vehicles = False else: booking_rego = BookingVehicleRego.objects.get( booking=old_booking, rego=n['rego']) n['booking'] = old_booking.id if booking_rego.type != n[ 'type'] or booking_rego.entry_fee != n[ 'entry_fee']: update_regos_serializers.append( BookingRegoSerializer(booking_rego, data=n)) # Create the new regos if they are there if regos_serializers: for r in regos_serializers: r.is_valid(raise_exception=True) r.save() # Update the new regos if they are there if update_regos_serializers: for r in update_regos_serializers: r.is_valid(raise_exception=True) r.save() same_vehicles = False # Check if there are regos in place that need to be removed stale_regos = [] for r in current_regos: if r.rego not in sent_regos: stale_regos.append(r.id) # delete stale regos if stale_regos: same_vehicles = False BookingVehicleRego.objects.filter( id__in=stale_regos).delete() else: same_vehicles = False if current_regos: current_regos.delete() if same_campsites and same_dates and same_vehicles and same_details: new_history.delete() return old_booking # Check difference of dates in booking old_booking_days = int( (old_booking.departure - old_booking.arrival).days) new_days = int((booking_details['end_date'] - booking_details['start_date']).days) date_diff = check_date_diff(old_booking, booking) total_price = price_or_lineitems(request, booking, booking_details['campsites'], lines=False, old_booking=old_booking) price_diff = True if old_booking.cost_total != total_price: price_diff = True if price_diff: booking = create_temp_bookingupdate(request, booking.arrival, booking.departure, booking_details, old_booking, total_price) # Attach campsite booking objects to old booking for c in booking.campsites.all(): c.booking = old_booking c.save() # Move all the vehicles to the in new booking to the old booking for r in booking.regos.all(): r.booking = old_booking r.save() old_booking.cost_total = booking.cost_total old_booking.departure = booking.departure old_booking.arrival = booking.arrival old_booking.details.update(booking.details) if not same_campground: old_booking.campground = booking.campground old_booking.save() booking.delete() delete_session_booking(request.session) send_booking_invoice(old_booking) # send out the confirmation email if the booking is paid or over paid if old_booking.status == 'Paid' or old_booking.status == 'Over Paid': send_booking_confirmation(old_booking, request) return old_booking except: delete_session_booking(request.session) print(traceback.print_exc()) raise