Ejemplo n.º 1
0
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, state="CONTROLLED")

    if request.method == "POST":
        if readOnly:
            # A bad user is playing with urls...
            return HttpResponseRedirect(reverse("core: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(reverse("expense: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": reverse('expense: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})
Ejemplo n.º 2
0
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})
Ejemplo n.º 3
0
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 perm.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)
            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_id in form.cleaned_data["expenses"]:
                    expense = Expense.objects.get(id=expense_id)
                    expense.expensePayment = expensePayment
                    expense.workflow_in_progress = False
                    expense.save()

            return HttpResponseRedirect(urlresolvers.reverse("expense.views.expense_payments"))
    else:
        if expense_payment_id:
            form = ExpensePaymentForm({"expenses": "|".join([str(e.id) for e in Expense.objects.filter(expensePayment=expensePayment)]), "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})
Ejemplo n.º 4
0
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})