Esempio n. 1
0
    def handle(self, *args, **kwargs):
        txn_set = defaultdict(list) 
        
        for ji in JournalItem.objects.all():
            txn_set[ji.txn_id].append(ji)
        for txn_id, ji_list in txn_set.iteritems():
            
            delete_list = []
            add_list = []
            net_change = 0

            if len(ji_list) == 1:
                continue
            for x in ji_list:
                if x.subscription.group.name == 'Personal Credits':
                    delete_list.append(x)
                    net_change += x.credit - x.debit
                if len(delete_list) > 1:
                    new_ji = JournalItem()
                    if(net_change > 0 ):
                        new_ji.credit = net_change
                        new_ji.debit = 0
                    else:
                        new_ji.debit = -net_change
                        new_ji.credit = 0
                    new_ji.subscription = delete_list[0].subscription
                    new_ji.txn = delete_list[0].txn
                    new_ji.save()

                    for x in delete_list:
                        print "Deleting: ", x.id
                        if x.id:
                            x.delete()
Esempio n. 2
0
def record_customer_payment(request):
    payment_amount = int(request.POST['payment_amount'])
    payments_for = {}
    for s in Subscriptions.objects.filter(
            member_id=request.POST['customer_id']):
        k = 'payment_for_' + str(s.id)
        if k in request.POST and request.POST[k]:
            payments_for[s.id] = int(request.POST[k])
    if sum(payments_for.values()) != payment_amount:
        return HttpResponse('Sum of amounts is not adding up')

    j = Journal()
    j.entry_date = request.POST['payment_date']
    j.comments = 'No Comments'
    j.entry_type = j.PAYMENT
    j.member_id = request.POST['customer_id']
    j.save()

    # Create due amounts after subtracting dividends
    for s_id, p_amount in iter(payments_for.items()):

        print(s_id, p_amount)

        j1 = JournalItem()
        j1.txn = j
        j1.debit = 0
        j1.credit = p_amount
        j1.subscription_id = s_id
        j1.save()

    # Redirect back to the page
    return HttpResponseRedirect('/customers/grouplist?id=' +
                                request.POST['customer_id'])
Esempio n. 3
0
def record_customer_payment(request):
    payment_amount = int(request.POST['payment_amount'])
    payments_for = {}
    for s in Subscriptions.objects.filter(member_id=request.POST['customer_id']):
        k = 'payment_for_'+str(s.id)
        if k in request.POST and request.POST[k]:
            payments_for[s.id] = int(request.POST[k])
    if sum(payments_for.values()) != payment_amount:
        return HttpResponse('Sum of amounts is not adding up')
    
    j = Journal()
    j.entry_date = request.POST['payment_date']
    j.comments = 'No Comments'
    j.entry_type = j.PAYMENT
    j.member_id = request.POST['customer_id']
    j.save()
        
    # Create due amounts after subtracting dividends
    for s_id, p_amount in payments_for.iteritems():
        
        print s_id, p_amount
        
        j1 = JournalItem()
        j1.txn = j
        j1.debit = 0
        j1.credit = p_amount
        j1.subscription_id = s_id
        j1.save()
    
    # Redirect back to the page 
    return HttpResponseRedirect('/customers/grouplist?id='+request.POST['customer_id'])
Esempio n. 4
0
def daily_collection(request):
    if (request.method == 'GET'):
        return get_daily_collection(request)

    if (request.method != 'POST'):
        return HttpResponse('Request must be either GET or POST', status=500)

    c_list = Customer.objects.all().order_by('name')
    for c in c_list:
        cust_payment = request.POST.get('customer_collection_%s' % c.id)
        cust_payment = int(cust_payment) if cust_payment else 0
        if cust_payment:
            print("Payment for ", c.name, " is ", cust_payment)
            print("Finding subscriptions with due amounts")
            dues = (JournalItem.objects.filter(subscription__member_id=c.id).
                    values('subscription_id').annotate(
                        Sum('debit'),
                        Sum('credit')).order_by('subscription__id'))

            # Get any previous credits
            pc_id = Subscriptions.objects.get(member_id=c.id, group_id=1).id
            a = JournalItem.objects.filter(subscription_id=pc_id).aggregate(
                Sum('debit'), Sum('credit'))
            credit_amount = int(a['credit__sum'] if a['credit__sum'] else 0)
            debit_amount = int(a['debit__sum'] if a['debit__sum'] else 0)
            old_credit = credit_amount - debit_amount

            # Create journal entry
            j = Journal()
            j.entry_date = request.POST['collection_date']
            j.amount = cust_payment
            j.entry_type = j.PAYMENT
            j.member = c
            j.save()

            # If there any old credit, add it to the current amount
            available_amount = old_credit + cust_payment
            for s in dues:
                s_id = s['subscription_id']
                due = s['debit__sum'] - s['credit__sum']
                if due <= 0:
                    continue

                j1 = JournalItem()
                j1.subscription_id = s_id
                j1.txn = j
                j1.debit = 0
                j1.credit = min(due, available_amount)
                j1.save()
                available_amount -= j1.credit
                if available_amount == 0:
                    break

            if available_amount != old_credit:
                j1 = JournalItem()
                j1.txn = j
                j1.credit = cust_payment
                j1.subscription_id = pc_id
                j1.debit = 0
                if old_credit < available_amount:
                    j1.credit = available_amount - old_credit
                else:
                    j1.debit = old_credit - available_amount
                j1.save()

    return HttpResponseRedirect('/customers/list')
Esempio n. 5
0
def new_auction(request):
    if request.method == 'GET':
        group = Group.objects.get(id=request.GET['id'])
        subscriptions_list = Subscriptions.objects.filter(
            group_id=request.GET['id'])
        subscription_count = Subscriptions.objects.filter(
            group_id=request.GET['id']).count()
        if subscription_count > group.total_months:
            return HttpResponse("Group Over subscribed")
        elif subscription_count < group.total_months:
            under_subscribed = True
        else:
            under_subscribed = False
        auction_month = sum(0 if s.auction_amount is None else 1
                            for s in subscriptions_list) + 1
        subscriptions_list = filter(lambda s: s.auction_amount is None,
                                    subscriptions_list)
        template = loader.get_template('groups/auction.html')
        context = {
            'subscriptions_list': subscriptions_list,
            'group': group,
            'auction_month': auction_month,
            'under_subscribed': under_subscribed
        }
        return HttpResponse(template.render(context))
    elif request.method == 'POST':
        # Mark the subscription for the auction
        s = Subscriptions.objects.get(id=request.POST['auctionmember'])
        s.auction_amount = float(request.POST['amount'])
        s.auction_date = request.POST['date']
        s.auction_number = request.POST['month']
        s.save()

        g = s.group
        if not g.started:
            g.started = True
            g.save()

        # Add missing subscriptions
        subscription_count = Subscriptions.objects.filter(
            group_id=g.id).count()
        missing_subscriptions = g.total_months - subscription_count
        if missing_subscriptions > 0:
            for i in range(missing_subscriptions):
                s1 = Subscriptions()
                s1.member_id = 1
                s1.group_id = g.id
                s1.comments = 'Automatically subscribed'
                s1.save()

        monthly_due = g.amount / g.total_months
        dividend = (s.auction_amount -
                    (g.amount * g.commision) / 100) / g.total_months
        due_amount = monthly_due - dividend

        # Create main journal Entries
        j = Journal()
        j.entry_date = s.auction_date
        j.comments = 'No Comments'
        j.entry_type = j.AUCTION
        j.save()

        # Create due amounts after subtracting dividends
        for s1 in Subscriptions.objects.filter(group_id=s.group_id):
            j1 = JournalItem()
            j1.txn = j
            j1.debit = due_amount
            j1.credit = 0
            j1.subscription_id = s1.id
            j1.save()

        return HttpResponseRedirect('/groups/members?id=' +
                                    request.POST['group_id'])
Esempio n. 6
0
def daily_collection(request):
    if(request.method=='GET'):
        return get_daily_collection(request)
    
    if(request.method != 'POST'):
        return HttpResponse('Request must be either GET or POST', status=500)

    c_list = Customer.objects.all().order_by('name')
    for c in c_list:
        cust_payment = request.POST.get('customer_collection_%s' % c.id)
        cust_payment = int(cust_payment) if cust_payment else 0
        if cust_payment:
            print "Payment for ", c.name, " is ", cust_payment
            print "Finding subscriptions with due amounts"
            dues = (JournalItem.objects
                               .filter(subscription__member_id=c.id)
                               .values('subscription_id')
                               .annotate(Sum('debit'),Sum('credit'))
                               .order_by('subscription__id'))
            
            # Get any previous credits
            pc_id = Subscriptions.objects.get(member_id=c.id, group_id=1).id
            a = JournalItem.objects.filter(subscription_id=pc_id).aggregate(Sum('debit'),Sum('credit'))
            credit_amount = int(a['credit__sum'] if a['credit__sum'] else 0)
            debit_amount = int(a['debit__sum'] if a['debit__sum'] else 0)
            old_credit = credit_amount - debit_amount

            # Create journal entry
            j = Journal()
            j.entry_date = request.POST['collection_date']
            j.amount = cust_payment
            j.entry_type = j.PAYMENT
            j.member = c
            j.save()
            
            # If there any old credit, add it to the current amount
            available_amount = old_credit + cust_payment

            print "\n"
            print "Total available amount == ", available_amount
            print "\n"

            for s in dues:
                s_id = s['subscription_id']
                due = s['debit__sum'] - s['credit__sum']
                if due <= 0:
                    continue
                
                j1 = JournalItem()
                j1.subscription_id = s_id
                j1.txn = j
                j1.debit = 0
                j1.credit = min(due, available_amount)
                j1.save()
                available_amount -= j1.credit
                if available_amount == 0:
                    break
            
            if available_amount != old_credit:
                j1 = JournalItem()
                j1.txn = j
                j1.credit = 0
                j1.subscription_id = pc_id
                j1.debit = 0
                if old_credit < available_amount:
                    j1.credit = available_amount - old_credit
                else:
                    j1.debit = old_credit - available_amount
                j1.save()

    return HttpResponseRedirect('/customers/list')
Esempio n. 7
0
def new_auction(request):
    if request.method == 'GET':
        group = Group.objects.get(id=request.GET['id'])
        subscriptions_list = Subscriptions.objects.filter(group_id=request.GET['id'])
        subscription_count = Subscriptions.objects.filter(group_id=request.GET['id']).count()
        if subscription_count > group.total_months:
            return HttpResponse("Group Over subscribed")
        elif subscription_count < group.total_months:
            under_subscribed = True
        else:
            under_subscribed = False
        auction_month = sum(0 if s.auction_amount is None else 1 for s in subscriptions_list)+1
        subscriptions_list = filter(lambda s:s.auction_amount is None, subscriptions_list)
        template = loader.get_template('groups/auction.html')
        context = {
            'subscriptions_list':subscriptions_list,
            'group':group,
            'auction_month':auction_month,
            'under_subscribed': under_subscribed 
        }
        return HttpResponse(template.render(context))
    elif request.method == 'POST':
        # Mark the subscription for the auction
        s = Subscriptions.objects.get(id=request.POST['auctionmember'])
        s.auction_amount = float(request.POST['amount'])
        s.auction_date = request.POST['date']
        s.auction_number = request.POST['month']
        s.save()
        
        g = s.group
        if not g.started:
            g.started = True
            g.save()
        
        # Add missing subscriptions
        subscription_count = Subscriptions.objects.filter(group_id=g.id).count()
        missing_subscriptions = g.total_months - subscription_count
        if missing_subscriptions > 0:
            for i in range(missing_subscriptions):
                s1 = Subscriptions()
                s1.member_id = 1
                s1.group_id = g.id
                s1.comments = 'Automatically subscribed'
                s1.save()

        monthly_due = g.amount / g.total_months
        dividend = (s.auction_amount - (g.amount * g.commision)/100) / g.total_months
        due_amount = monthly_due - dividend
        
        # Create main journal Entries
        j = Journal()
        j.entry_date = s.auction_date
        j.comments = 'No Comments'
        j.entry_type = j.AUCTION
        j.save()
        
        # Create due amounts after subtracting dividends
        for s1 in Subscriptions.objects.filter(group_id=s.group_id):
            j1 = JournalItem()
            j1.txn = j
            j1.debit = due_amount
            j1.credit = 0
            j1.subscription_id = s1.id
            j1.save()
        
        return HttpResponseRedirect('/groups/members?id='+ request.POST['group_id']) 
Esempio n. 8
0
def daily_collection(request):
    c_list = Customer.objects.all().order_by('name')
    for c in c_list:
        cust_payment = request.POST.get('customer_collection_%s' % c.id)
        cust_payment = int(cust_payment) if cust_payment else 0
        if cust_payment:
            print "Payment for ", c.name, " is ", cust_payment
            print "Finding subscriptions with due amounts"
            dues = (JournalItem.objects
                               .filter(subscription__member_id=c.id)
                               .values('subscription_id')
                               .annotate(Sum('debit'),Sum('credit'))
                               .order_by('subscription__id'))
            
            # Get any previous credits
            pc_id = Subscriptions.objects.get(member_id=c.id, group_id=1).id
            a = JournalItem.objects.filter(subscription_id=pc_id).aggregate(Sum('debit'),Sum('credit'))
            credit_amount = int(a['credit__sum'] if a['credit__sum'] else 0)
            debit_amount = int(a['debit__sum'] if a['debit__sum'] else 0)
            old_credit = credit_amount - debit_amount

            # Create journal entry
            j = Journal()
            j.entry_date = request.POST['collection_date']
            j.amount = cust_payment
            j.entry_type = j.PAYMENT
            j.save()
            
            # If there any old credit, add it to the current amount
            if old_credit != 0:
                j1 = JournalItem()
                j1.txn = j
                j1.subscription_id = pc_id
                cust_payment += old_credit

                if(old_credit>0):
                    j1.debit, j1.credit = old_credit, 0
                    j1.credit = 0
                else:
                    # We don't expect this case.  But if the customer
                    # Owe us something for what ever reason, we reduce it
                    # from the amount
                    j1.debit, j1.credit = 0, -old_credit
                j1.save()
            
            for s in dues:
                s_id = s['subscription_id']
                due = s['debit__sum'] - s['credit__sum']
                if due == 0:
                    continue
                
                j1 = JournalItem()
                j1.subscription_id = s_id
                j1.txn = j
                j1.debit = 0
                j1.credit = min(due, cust_payment)
                j1.save()
                cust_payment -= j1.credit
                if cust_payment == 0:
                    break
            else:
                j1 = JournalItem()
                j1.txn = j
                j1.credit = cust_payment
                j1.subscription_id = pc_id
                j1.debit = 0
                j1.save()

    return HttpResponseRedirect('/customers/list')
Esempio n. 9
0
def daily_collection(request):
    c_list = Customer.objects.all().order_by('name')
    for c in c_list:
        cust_payment = request.POST.get('customer_collection_%s' % c.id)
        cust_payment = int(cust_payment) if cust_payment else 0
        if cust_payment:
            print "Payment for ", c.name, " is ", cust_payment
            print "Finding subscriptions with due amounts"
            dues = (JournalItem.objects.filter(subscription__member_id=c.id).
                    values('subscription_id').annotate(
                        Sum('debit'),
                        Sum('credit')).order_by('subscription__id'))

            # Get any previous credits
            pc_id = Subscriptions.objects.get(member_id=c.id, group_id=1).id
            a = JournalItem.objects.filter(subscription_id=pc_id).aggregate(
                Sum('debit'), Sum('credit'))
            credit_amount = int(a['credit__sum'] if a['credit__sum'] else 0)
            debit_amount = int(a['debit__sum'] if a['debit__sum'] else 0)
            old_credit = credit_amount - debit_amount

            # Create journal entry
            j = Journal()
            j.entry_date = request.POST['collection_date']
            j.amount = cust_payment
            j.entry_type = j.PAYMENT
            j.save()

            # If there any old credit, add it to the current amount
            if old_credit != 0:
                j1 = JournalItem()
                j1.txn = j
                j1.subscription_id = pc_id
                cust_payment += old_credit

                if (old_credit > 0):
                    j1.debit, j1.credit = old_credit, 0
                    j1.credit = 0
                else:
                    # We don't expect this case.  But if the customer
                    # Owe us something for what ever reason, we reduce it
                    # from the amount
                    j1.debit, j1.credit = 0, -old_credit
                j1.save()

            for s in dues:
                s_id = s['subscription_id']
                due = s['debit__sum'] - s['credit__sum']
                if due == 0:
                    continue

                j1 = JournalItem()
                j1.subscription_id = s_id
                j1.txn = j
                j1.debit = 0
                j1.credit = min(due, cust_payment)
                j1.save()
                cust_payment -= j1.credit
                if cust_payment == 0:
                    break
            else:
                j1 = JournalItem()
                j1.txn = j
                j1.credit = cust_payment
                j1.subscription_id = pc_id
                j1.debit = 0
                j1.save()

    return HttpResponseRedirect('/customers/list')