Exemple #1
0
def addItem_aa(request, bookId):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(getHost(request) + '/login/?next=%s' % request.path)  
    if request.method == 'POST' :
        #contactIds = request.REQUEST.getList("contact")
        form = ItemForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            billItems = []
            g = Bill(title=cd['title'], description=cd['description'], book=Book(pk=bookId), date=cd['date'], create_by=request.user, amount=cd['amount'])
            g.save()
            for contactId in request.POST["contactId0s"].strip(",").split(","):
                subAmount = request.POST['subCount0_'+contactId]
                billItems.append(Bill_Item(contact=Contact(pk=contactId), amount=subAmount, bill=g))

            for contactId in request.POST["contactIds"].strip(",").split(","):
                subAmount = -float(request.POST['subCount_'+contactId])
                billItems.append(Bill_Item(contact=Contact(pk=contactId), amount=subAmount, bill=g))
            for billItem in billItems:
                billItem.save()
            return HttpResponseRedirect(getHost(request) + "/books/" + bookId + "/")
    else:
        form = ItemForm(initial={'type':'1'})
    contacts = [contactBook.contact for contactBook in Contact_Book.objects.filter(book=Book(pk=bookId))]
    return  render_to_response('books/item_form_aa.html', {'form':form, 'contacts':contacts},context_instance=RequestContext(request))
Exemple #2
0
def balance(request, bookId):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(getHost(request) + '/login/?next=%s' % request.path)  
    book = Book.objects.get(id=bookId)
    bills = book.bill_set.all()
    dic = {}
    for bill in bills:
        for item in bill.bill_item_set.all():
            if dic.has_key(item.contact.id):
                dic[item.contact.id] += item.amount
            else:
                dic[item.contact.id] = item.amount
    num = 0
    g = Bill(title='结账', book=book, create_by=request.user)
    billItems = []
    for k, v in dic.items():
        if v > 0:
            num +=v
        billItems.append(Bill_Item(contact=Contact(pk=k), amount=-v))
    g.amount = num
    g.save()    
    for billItem in billItems:
        billItem.bill = g
        billItem.save()
    
    return bookDetail(request, bookId)