Beispiel #1
0
def prepare(request, create=False):
    member_id = request.POST['member_id']

    invoice_number_max = Invoice.objects.all().aggregate(
        Max('invoice_number'))['invoice_number__max'] or 0
    invoice_number = invoice_number_max + 1

    member = Member.objects.get(pk=member_id)
    usages = Usage.objects.filter(member=member, invoice=None)

    total_amount = usages.aggregate(total=Sum('total_price'))['total'] or 0

    usages_annotated = usages.values(
        'resource__name', 'resource__unit__name', 'comment', 'unit_price',
        'project__name').annotate(
            qty=Sum('qty'),
            total_price=Sum('total_price')).order_by('project__name')

    balance = AccountEntry.objects.filter(member=member).aggregate(
        machine=Sum('amount_machine'), cash=Sum('amount_cash'))

    # information about machine hours balance
    amount_machine_before = balance['machine'] or 0
    amount_machine_usages = usages.filter(
        resource__payable_by_animation_hours=True).aggregate(
            total=Sum('total_price'))['total'] or 0
    amount_other_usages = total_amount - amount_machine_usages

    deduction_machine = min(amount_machine_before, amount_machine_usages)
    amount_machine_after = amount_machine_before - deduction_machine

    # information about cash balance
    cash_before = balance['cash'] or 0
    remaining_amount = total_amount - deduction_machine
    deduction_cash = min(cash_before, remaining_amount)
    cash_after = cash_before - deduction_cash

    deduction = deduction_machine + deduction_cash

    invoice = Invoice(amount=total_amount,
                      amount_deduction_machine=deduction_machine,
                      amount_deduction_cash=deduction_cash,
                      member=member,
                      invoice_number=invoice_number)

    print(total_amount)
    if create:
        invoice.save()

        if deduction > 0:
            AccountEntry.objects.create(member=member,
                                        amount_machine=-deduction_machine,
                                        amount_cash=-deduction_cash,
                                        invoice=invoice)

        invoice.date_invoice = timezone.now()
        invoice.save()
        usages.update(invoice=invoice)
        usages = Usage.objects.filter(invoice=invoice)

    return {
        'usages': usages,
        'usages_anotated': usages_annotated,
        'member_info': member,
        'invoice': invoice,
        'amount_machine_before': amount_machine_before,
        'amount_machine_after': amount_machine_after,
        'amount_machine_usages': amount_machine_usages,
        'amount_other_usages': amount_other_usages,
        'deduction_machine': deduction_machine,
        'cash_before': cash_before,
        'cash_after': cash_after,
        'deduction_cash': deduction_cash,
        'site_title': 'Invoice preview'
    }