Esempio n. 1
0
def record_bill_unequal_split(request):
    userprofile_object = UserProfile.objects.get(user=request.user)
    my_friends = UserFriend.objects.filter(user_profile=userprofile_object)
    number_of_friends = my_friends.count()
    if request.method == 'POST':
        bill_form = PartialBillForm(request.POST)
        if bill_form.is_valid():
            overall_bill_id = create_overall_bill_id()
            lender = userprofile_object
            bill = Bill(overall_bill_id=overall_bill_id, lender=lender, date=bill_form.cleaned_data['date'], description=bill_form.cleaned_data['description'], amount=bill_form.cleaned_data['amount'])
            bill.save()

            # To count total people involved
            friends_total = 0
            for i in range(0, number_of_friends):
                if request.POST['borrower_'+str(i)] != '':
                    friends_total += 1
            new_friends_total = len(request.POST.getlist('people_new'))
            total_people = friends_total + new_friends_total

            # For existing friends
            for i in range(0, number_of_friends):
                borrower_counter = 'borrower_' + str(i)
                number_of_people_counter = 'number_of_people_' + str(i)
                borrower_amount_counter = 'borrower_amount_' + str(i)
                try:
                    if request.POST[borrower_counter] != '':
                        billdetail_id = create_billdetail_id()
                        if request.POST[borrower_counter] != userprofile_object.user.email:
                            borrower = UserFriend.objects.get(user_profile=userprofile_object, friend_email=request.POST[borrower_counter])
                            bill_detail = BillDetails(billdetail_id=billdetail_id, bill=bill, borrower=borrower, individual_amount=float(request.POST[borrower_amount_counter]), bill_cleared='N')
                            bill_detail.save()
                            # Send Email Start
                            context = Context({ 'userprofile_object': userprofile_object, 'bill': bill, 'bill_detail': bill_detail })
                            subject = 'New Bill Recorded: ' + bill.description
                            bill_creation_txt_content = bill_creation_txt.render(context)
                            bill_creation_html_content = bill_creation_html.render(context)
                            send_html_mail(subject, bill_creation_txt_content, bill_creation_html_content, settings.DEFAULT_FROM_EMAIL, [ request.POST[borrower_counter] ])
                            # Send Email End
                        else:
                            borrower = UserFriend.objects.get(user_profile=userprofile_object, friend_email=userprofile_object.user.email)
                            bill_detail = BillDetails(billdetail_id=billdetail_id, bill=bill, borrower=borrower, individual_amount=float(request.POST[borrower_amount_counter]), bill_cleared='Y')
                            bill_detail.save()
                    else:
                        continue
                except:
                    continue

            # For new friends
            if request.POST.getlist('people_new'):
                for borrower in request.POST.getlist('people_new'):
                    i = borrower[13:]
                    friend_name = request.POST['x_name_'+str(i)]
                    friend_email = request.POST['x_email_'+str(i)]
                    number_of_people = request.POST['x_number_of_people_'+str(i)]
                    borrower_amount = request.POST['x_borrower_amount_'+str(i)]
                    if friend_name and friend_email and number_of_people and borrower_amount:
                        try:
                            user_friend = UserFriend.objects.get(user_profile=userprofile_object, friend_email=friend_email)
                        except:
                            userfriend_id = create_userfriend_id()
                            user_friend = UserFriend(userfriend_id=userfriend_id, user_profile=userprofile_object, friend_name=friend_name, friend_email=friend_email)
                            user_friend.save()
                        borrower_object = user_friend
                        billdetail_id = create_billdetail_id()
                        bill_detail = BillDetails(billdetail_id=billdetail_id, bill=bill, borrower=borrower_object, individual_amount=borrower_amount, bill_cleared='N')

                        bill_detail.save()
                        
                        # Send Email Start
                        context = Context({ 'userprofile_object': userprofile_object, 'bill': bill, 'bill_detail': bill_detail })
                        subject = 'New Bill Recorded: ' + bill.description
                        bill_creation_txt_content = bill_creation_txt.render(context)
                        bill_creation_html_content = bill_creation_html.render(context)
                        send_html_mail(subject, bill_creation_txt_content, bill_creation_html_content, settings.DEFAULT_FROM_EMAIL, [ friend_email ])
                        # Send Email End

            return HttpResponseRedirect('/who-owes-me/')
    else: 
        bill_form = PartialBillForm()
        return render_to_response('record-bill-unequal-split.html', { 'my_friends': my_friends, 'bill_form': bill_form, 'userprofile_object': userprofile_object, 'request': request }, context_instance=RequestContext(request))
Esempio n. 2
0
def record_bill_equal_split(request):
    userprofile_object = UserProfile.objects.get(user=request.user)
    if request.method == 'POST':
        bill_form = PartialBillForm(request.POST)
        if bill_form.is_valid():
            overall_bill_id = create_overall_bill_id()
            lender = userprofile_object
            bill = Bill(overall_bill_id=overall_bill_id, lender=lender, date=bill_form.cleaned_data['date'], description=bill_form.cleaned_data['description'], amount=bill_form.cleaned_data['amount'])
            bill.save()
            number_of_borrowers = len(request.POST.getlist('people')) + len(request.POST.getlist('people_new'))
            individual_amount = float(bill_form.cleaned_data['amount']/number_of_borrowers)

            # For already existing friends
            if request.POST.getlist('people'):
                for borrower in request.POST.getlist('people'):
                    billdetail_id = create_billdetail_id()
                    if borrower != userprofile_object.user.email:
                        borrower_object = UserFriend.objects.get(user_profile=userprofile_object, friend_email=borrower)
                        bill_detail = BillDetails(billdetail_id=billdetail_id, bill=bill, borrower=borrower_object, individual_amount=individual_amount, bill_cleared='N')
                        bill_detail.save()
                        # Send Email Start
                        context = Context({ 'userprofile_object': userprofile_object, 'bill': bill, 'bill_detail': bill_detail })
                        subject = 'New Bill Recorded: ' + bill.description
                        bill_creation_txt_content = bill_creation_txt.render(context)
                        bill_creation_html_content = bill_creation_html.render(context)
                        send_html_mail(subject, bill_creation_txt_content, bill_creation_html_content, settings.DEFAULT_FROM_EMAIL, [ borrower ])
                        # Send Email End
                    else:
                        borrower_object = UserFriend.objects.get(user_profile=userprofile_object, friend_email=userprofile_object.user.email)
                        bill_detail = BillDetails(billdetail_id=billdetail_id, bill=bill, borrower=borrower_object, individual_amount=individual_amount, bill_cleared='Y')
                        bill_detail.save()
            
            # For new friends
            if request.POST.getlist('people_new'):
                for borrower in request.POST.getlist('people_new'):
                    i = borrower[11:]
                    friend_name = request.POST['name_'+str(i)]
                    friend_email = request.POST['email_'+str(i)]
                    try:
                        user_friend = UserFriend.objects.get(user_profile=userprofile_object, friend_email=friend_email)
                    except:
                        userfriend_id = create_userfriend_id()
                        user_friend = UserFriend(userfriend_id=userfriend_id, user_profile=userprofile_object, friend_name=friend_name, friend_email=friend_email)
                        user_friend.save()
                    borrower_object = user_friend
                    billdetail_id = create_billdetail_id()
                    bill_detail = BillDetails(billdetail_id=billdetail_id, bill=bill, borrower=borrower_object, individual_amount=individual_amount, bill_cleared='N')
                    bill_detail.save()

                    # Send Email Start
                    context = Context({ 'userprofile_object': userprofile_object, 'bill': bill, 'bill_detail': bill_detail })
                    subject = 'New Bill Recorded: ' + bill.description
                    bill_creation_txt_content = bill_creation_txt.render(context)
                    bill_creation_html_content = bill_creation_html.render(context)
                    send_html_mail(subject, bill_creation_txt_content, bill_creation_html_content, settings.DEFAULT_FROM_EMAIL, [ friend_email ])
                    # Send Email End

            return HttpResponseRedirect('/who-owes-me/')
    else:
        my_friends = UserFriend.objects.filter(user_profile=userprofile_object)
        bill_form = PartialBillForm()
        return render_to_response('record-bill-equal-split.html', { 'my_friends': my_friends, 'bill_form': bill_form, 'userprofile_object': userprofile_object, 'request': request }, context_instance=RequestContext(request))