def expenses_history(request): """Display expense history. @param year: year of history. If None, display recent items and year index""" expenses = Expense.objects.all().select_related().prefetch_related( "clientbill_set", "user", "lead") try: consultant = Consultant.objects.get( trigramme__iexact=request.user.username) user_team = consultant.userTeam() except Consultant.DoesNotExist: user_team = [] if not perm.has_role(request.user, "expense paymaster"): expenses = expenses.filter( Q(user=request.user) | Q(user__in=user_team)) expenseTable = ExpenseTable(expenses, orderable=True) RequestConfig(request, paginate={"per_page": 50}).configure(expenseTable) if "csv" in request.GET: return tableToCSV(expenseTable, filename="expenses.csv") return render(request, "expense/expense_archive.html", { "expense_table": expenseTable, "user": request.user })
def expense_payment_detail(request, expense_payment_id): """Display detail of this expense payment""" expense_administrator, expense_manager, expense_paymaster, expense_requester = user_expense_perm( request.user) if not expense_requester: return HttpResponseRedirect(reverse("core:forbiden")) try: if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) if not (expensePayment.user() == request.user or expense_paymaster or expense_administrator): return HttpResponseRedirect(reverse("core:forbiden")) except ExpensePayment.DoesNotExist: messages.add_message( request, messages.ERROR, _("Expense payment %s does not exist" % expense_payment_id)) return redirect("expense:expense_payments") return render( request, "expense/expense_payment_detail.html", { "expense_payment": expensePayment, "expense_table": ExpenseTable(expensePayment.expense_set.all()), "can_edit_vat": expense_administrator or expense_paymaster, "user": request.user })
def expense_payments(request, expense_payment_id=None): readOnly = False if not request.user.groups.filter(name="expense_paymaster").exists() and not request.user.is_superuser: readOnly = True try: if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) except ExpensePayment.DoesNotExist: messages.add_message(request, messages.ERROR, _("Expense payment %s does not exist" % expense_payment_id)) expense_payment_id = None expensePayment = None if readOnly: expensesToPay = [] else: expensesToPay = Expense.objects.filter(workflow_in_progress=True, corporate_card=False, expensePayment=None) expensesToPay = [expense for expense in expensesToPay if wf.get_state(expense).transitions.count() == 0] if request.method == "POST": if readOnly: # A bad user is playing with urls... return HttpResponseRedirect(urlresolvers.reverse("forbiden")) form = ExpensePaymentForm(request.POST) if form.is_valid(): if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) expensePayment.payment_date = form.cleaned_data["payment_date"] else: expensePayment = ExpensePayment(payment_date=form.cleaned_data["payment_date"]) expensePayment.save() for expense in Expense.objects.filter(expensePayment=expensePayment): expense.expensePayment = None # Remove any previous association expense.save() if form.cleaned_data["expenses"]: for expense in form.cleaned_data["expenses"]: expense.expensePayment = expensePayment expense.workflow_in_progress = False expense.save() return HttpResponseRedirect(urlresolvers.reverse("expense.views.expense_payments")) else: print "form is not valid" else: if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) form = ExpensePaymentForm({"expenses": list(Expense.objects.filter(expensePayment=expensePayment).values_list("id", flat=True)), "payment_date": expensePayment.payment_date}) # A form that edit current expense payment else: form = ExpensePaymentForm(initial={"payment_date": date.today()}) # An unbound form return render(request, "expense/expense_payments.html", {"modify_expense_payment": bool(expense_payment_id), "data_url": urlresolvers.reverse('expense_payment_table_DT'), "data_options": ''' "pageLength": 25, "order": [[0, "desc"]], "columnDefs": [{ "orderable": false, "targets": [1, 2, 4] }]''', "expense_to_pay_table": ExpenseTable(expensesToPay), "read_only": readOnly, "form": form, "user": request.user})
def lead_expenses(request, lead_id): """Page fragment or csv that display expenses related to given lead""" try: lead = Lead.objects.get(id=lead_id) expenses = Expense.objects.filter(lead=lead).select_related().prefetch_related("clientbill_set") except Lead.DoesNotExist: expenses = [] if "csv" in request.GET: expenseTable = ExpenseTable(expenses, orderable=True) RequestConfig(request, paginate={"per_page": 50}).configure(expenseTable) return tableToCSV(expenseTable, filename="expenses.csv") return render(request, "expense/expense_list.html", {"expenses": expenses, "lead": lead, "user": request.user})
def expense_payment_detail(request, expense_payment_id): """Display detail of this expense payment""" if not request.user.groups.filter(name="expense_requester").exists(): return HttpResponseRedirect(urlresolvers.reverse("forbiden")) try: if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) if not (expensePayment.user() == request.user or\ utils.has_role(request.user, "expense paymaster") or\ utils.has_role(request.user, "expense manager")): return HttpResponseRedirect(urlresolvers.reverse("forbiden")) except ExpensePayment.DoesNotExist: messages.add_message(request, messages.ERROR, _("Expense payment %s does not exist" % expense_payment_id)) return redirect(expense_payments) return render(request, "expense/expense_payment_detail.html", {"expense_payment": expensePayment, "expense_table": ExpenseTable(expensePayment.expense_set.all()), "user": request.user})
def expense_payments(request, expense_payment_id=None): readOnly = False if not request.user.groups.filter(name="expense_paymaster").exists() and not request.user.is_superuser: readOnly = True try: if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) except ExpensePayment.DoesNotExist: messages.add_message(request, messages.ERROR, _("Expense payment %s does not exist" % expense_payment_id)) expense_payment_id = None expensePayment = None if readOnly: expensesToPay = [] else: expensesToPay = Expense.objects.filter(workflow_in_progress=True, corporate_card=False, expensePayment=None) expensesToPay = [expense for expense in expensesToPay if wf.get_state(expense).transitions.count() == 0] try: consultant = Consultant.objects.get(trigramme__iexact=request.user.username) user_team = consultant.userTeam() except Consultant.DoesNotExist: user_team = [] expensePayments = ExpensePayment.objects.all() if not utils.has_role(request.user, "expense paymaster"): expensePayments = expensePayments.filter(Q(expense__user=request.user) | Q(expense__user__in=user_team)).distinct() if request.method == "POST": if readOnly: # A bad user is playing with urls... return HttpResponseRedirect(urlresolvers.reverse("forbiden")) form = ExpensePaymentForm(request.POST) if form.is_valid(): if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) expensePayment.payment_date = form.cleaned_data["payment_date"] else: expensePayment = ExpensePayment(payment_date=form.cleaned_data["payment_date"]) expensePayment.save() for expense in Expense.objects.filter(expensePayment=expensePayment): expense.expensePayment = None # Remove any previous association expense.save() if form.cleaned_data["expenses"]: for expense in form.cleaned_data["expenses"]: expense.expensePayment = expensePayment expense.workflow_in_progress = False expense.save() return HttpResponseRedirect(urlresolvers.reverse("expense.views.expense_payments")) else: print "form is not valid" else: if expense_payment_id: expensePayment = ExpensePayment.objects.get(id=expense_payment_id) form = ExpensePaymentForm({"expenses": list(Expense.objects.filter(expensePayment=expensePayment).values_list("id", flat=True)), "payment_date": expensePayment.payment_date}) # A form that edit current expense payment else: form = ExpensePaymentForm(initial={"payment_date": date.today()}) # An unbound form return render(request, "expense/expense_payments.html", {"modify_expense_payment": bool(expense_payment_id), "expense_payment_table": ExpensePaymentTable(expensePayments), "expense_to_pay_table": ExpenseTable(expensesToPay), "read_only": readOnly, "form": form, "user": request.user})