def cam_save_booking_details(request):
    if not request.user.is_authenticated():
        return redirect('/corporate/')
    sid = transaction.savepoint()
    #pdb.set_trace()
    try:
        print 'Request Accepted '
        store_user_track(request, "Customer Saving Booking details")   # for tracking user activity
        if request.method == "POST":
            v_cam_id        = request.session['user_id']
            guest_name      = request.POST.get('guest_name')
            guest_phone     = request.POST.get('phone_number')
            v_guest_email   = request.POST.get('email_id')
            var_property_id = request.POST.get('property_id')
            occupancy_type  = int(request.POST.get('occupancy_type'))
            check_in    = datetime.datetime.strptime(request.POST.get('check_in'), '%d/%m/%Y')
            check_out   = datetime.datetime.strptime(request.POST.get('check_out'), '%d/%m/%Y')
            promocode   = request.POST.get('promocode')
            #amount      = request.POST.get('amount')
            #taxes       = request.POST.get('taxes')
            gross_amount= request.POST.get('gross_amount')
            
            guest_obj = Guest.objects.get(guest_id=guest_name)
            guest_obj.guest_contactno     = guest_phone
            guest_obj.guest_email         = v_guest_email
            guest_obj.save()
            
            # Getting Property Object
            property_obj = Property.objects.get(property_id=var_property_id)
            
            # Default Property Rate if it is not decided for CAM.
            default_rate = PropertyRate.objects.get(property_id=property_obj)
            occupancy_rate =0
        
            # Customer Object
            cam_obj= Customer.objects.get(id=v_cam_id)
            # This is for fetching property rates and applying rates according to occupancy type
            property_rates = CAMPropertyRate.objects.filter(property_id=property_obj, cust_id=cam_obj)
            if property_rates:
                for rate in property_rates:
                    if int(rate.occupancy_type) == int(occupancy_type):
                        occupancy_rate = rate.agreed_rate
            else:
                if occupancy_type == 0:
                    occupancy_rate = default_rate.single_occupancy_display_rate
                else:
                    occupancy_rate = default_rate.double_occupancy_display_rate
            
            days = check_out - check_in
            if days.days == 0:
                day = 1
            else:
                day = days.days
                
            booking_obj = Booking(
                customer_id      = cam_obj,
                guest_id    = guest_obj,
                property_id   = property_obj,
                booking_estimated_checkin_date = check_in,
                booking_actual_checkin_date = check_in,
                booking_estimated_checkout_date = check_out,
                booking_actual_checkout_date = check_out,
                booking_estimated_checkout_time = datetime.datetime.now().time(),
                booking_estimated_checkin_time = datetime.datetime.now().time(),
                booking_estimated_no_of_day_stay = days.days,
                booking_actual_no_of_day_stay = day,
                booking_status  = BOOKING_OPEN, #BOOKING_BOOKED
                booking_rate    = occupancy_rate
            )
            booking_obj.save()
            booking_obj.booking_unique_id = 'BK' +  datetime.date.today().strftime('%d%m%y') + 'G' + str(guest_obj.guest_id) + str(booking_obj.booking_id).zfill(6)
            booking_obj.save()
            
            # Generating Invoice
            
            amount = occupancy_rate * day
            service_tax_amount = ( amount * SERVICE_TAX )/100
            luxury_tax_amount  = ( amount * LUXURY_TAX )/100
        
            tax_amount = service_tax_amount + luxury_tax_amount
            gross_amount = amount + tax_amount
            invoice = Invoice(
                booking_id = booking_obj,
                room_charges = amount,
                invoice_total_amount = amount,
                tax_amount  = tax_amount,
                invoice_gross_amount = gross_amount,
                invoice_status = 0,
            )
            invoice.save()
            invoice.invoice_unique_id = 'INVBK' + str(booking_obj.booking_id) + str(invoice.invoice_id).zfill(6)
            invoice.save()
            
            corporate_transaction = CorporateTransaction(
                invoice_id      = invoice.invoice_unique_id,
                corporate_id    = cam_obj,
                transaction_amount= invoice.invoice_gross_amount,
                transaction_type = 1
            )
            corporate_transaction.save()
            
            # This is for Booking Statistics added on 11 Dec 2015
            booking_stat = UserBookingStatisticTrack(
                user_id = cam_obj,
                booking_path = request.path,
                booking_date = datetime.date.today(),
                booking_id = booking_obj
            )
            booking_stat.save()
            transaction.savepoint_commit(sid)
            try:
                # Sending SMS to vendot regarding booking submission
                send_booking_submission_sms_to_vendor(property_obj.property_owner_id.cust_contact_no, property_obj.property_owner_id.cust_first_name,
                    booking_obj.booking_unique_id, property_obj.property_actual_name,  booking_obj.booking_id)
                # Sending Booking Submission SMS to Corporate User
                send_booking_submission_sms_to_customer(cam_obj.cust_contact_no, cam_obj.cust_first_name, booking_obj.booking_unique_id)
                # Sending Booking Submission Email to Corporate User
                send_booking_submission_mail_with_template(booking_obj.booking_id)
            except Exception as err:
                print 'SMS/ Email not sent to vendor/ customer / '
            return redirect('/corporate/cam-booking-details/?booking_id='+ str(booking_obj.booking_unique_id))
        else:
            print 'Hello'
            transaction.savepoint_rollback(sid)
            data = {'success':'false', 'error_message':'Invalid Request'}
    except Exception, e:
        print 'Error ', e
        transaction.savepoint_rollback(sid)
        data = {'success':'false', 'error_message':'Server Error- Please Try Again'}
def save_quote_booking(request):
    #pdb.set_trace()
    print 'Hello'
    sid = transaction.savepoint()
    try:
        store_user_track(request, 'Customer Doing Booking By Accepting Quotation') # for tracking user activit

        quotation = QuotationResponse.objects.get(quotation_uid=request.POST.get('quote_uid'))
        
        guest_list = request.POST.get('guest_list')
        guest_list = guest_list.split(',')
        print guest_list
        guest_list = [x for x in guest_list if x] # This removes blank values or empty elements
        
        if len(guest_list)>=1:
            guest_obj = Guest.objects.get(guest_id=guest_list[0])
        else:
            guest_obj = None
        
        days = quotation.request_id.quote_end_date - quotation.request_id.quote_start_date
        booking_obj = Booking(
            customer_id      = quotation.request_id.customer_id,
            guest_id    = guest_obj,
            property_id   = quotation.property_id,
            booking_estimated_checkin_date  = quotation.request_id.quote_start_date,
            booking_actual_checkin_date     = quotation.request_id.quote_start_date,
            booking_estimated_checkout_date = quotation.request_id.quote_end_date,
            booking_actual_checkout_date    = quotation.request_id.quote_end_date,
            booking_estimated_checkout_time = datetime.datetime.now().time(),
            booking_estimated_checkin_time  = datetime.datetime.now().time(),
            booking_estimated_no_of_day_stay = days.days,
            booking_actual_no_of_day_stay   = days.days,
            number_of_person = quotation.request_id.quote_no_of_guest,
            booking_status   = BOOKING_OPEN, #BOOKING_BOOKED
            booking_rate    = quotation.rate,
            booking_amount  = quotation.total_quote_amt,
            is_from_quote   = True,
            quotation_id    = quotation.quotation_uid
        )
        booking_obj.save()
        booking_obj.booking_unique_id = 'BK' +  datetime.date.today().strftime('%d%m%y') + 'Q' + str(quotation.quotation_id) + str(booking_obj.booking_id).zfill(6)
        booking_obj.remark = 'Guest List : ' + ','.join(guest_list)
        booking_obj.save()
        
        # Generating Invoice
        invoice = Invoice(
            booking_id = booking_obj,
            room_charges = quotation.amount,
            invoice_total_amount =  quotation.amount,
            tax_amount  = quotation.tax_amount,
            invoice_gross_amount = quotation.total_quote_amt,
            invoice_status = 0,
        )
        invoice.save()
        invoice.invoice_unique_id = 'INVBK' + str(booking_obj.booking_id) + str(invoice.invoice_id).zfill(6)
        invoice.save()

        corporate_transaction = CorporateTransaction(
            invoice_id      = invoice.invoice_unique_id,
            corporate_id    = quotation.request_id.customer_id,
            transaction_amount= invoice.invoice_gross_amount,
            transaction_type = 1
        )
        corporate_transaction.save()
        
        # This is for Booking Statistics added on 11 Dec 2015
        booking_stat = UserBookingStatisticTrack(
            user_id = quotation.request_id.customer_id,
            booking_path = request.path,
            booking_date = datetime.date.today(),
            booking_id = booking_obj
        )
        booking_stat.save()
        quotation.quotation_status = 'BOOKED'
        quotation.save()
        transaction.savepoint_commit(sid)

        try:
            # Sending Booking Submission SMS to Corporate User
            send_booking_submission_sms_to_customer(quotation.request_id.customer_id.cust_contact_no, quotation.request_id.customer_id.cust_first_name, booking_obj.booking_unique_id)
            
            store_user_track(request, 'Booking Submission SMS Sent To Customer') # for tracking user activity
            # SMS to VENDOR
            send_booking_submission_sms_to_vendor(quotation.property_id.property_owner_id.cust_contact_no,
                quotation.property_id.property_owner_id.cust_first_name, booking_obj.booking_unique_id, quotation.property_id.property_actual_name,  booking_obj.booking_id)
            
            store_user_track(request, 'Booking Submission SMS Sent To Vendor') # for tracking user activity
            # Sending Booking Submission Email to Corporate User
            send_booking_submission_mail_with_template(booking_obj.booking_id)
            
            data = { 'success':'true', 'booking_uid': booking_obj.booking_unique_id }
        except Exception as err:
            store_user_track(request, 'Booking Submission SMS or Email Not Sent To Vendor and Corporate') # for tracking user activity
            print 'Sending SMS or Email not Sent to Corporate Customer : ',err
            data = { 'success':'true', 'booking_uid': booking_obj.booking_unique_id }
    except Exception as err:
        print 'Exception : ',err
        transaction.savepoint_rollback(sid)
        data = {'success':'false', 'error_message':'Server Error- Please Try Again'}
    return HttpResponse(json.dumps(data), content_type='application/json')