Beispiel #1
0
def invoice_edit(request, invoice_id):
    invoice_obj = get_object_or_404(Invoice, pk=invoice_id)

    if not (request.user.role == 'ADMIN' or request.user.is_superuser or invoice_obj.created_by == request.user or request.user in invoice_obj.assigned_to.all()):
        raise PermissionDenied

    if request.method == 'GET':
        context = {}
        context['invoice_obj'] = invoice_obj
        context['form'] = InvoiceForm(
            instance=invoice_obj, request_user=request.user)
        context['from_address_form'] = InvoiceAddressForm(prefix='from',
                                                          instance=invoice_obj.from_address)
        context['to_address_form'] = InvoiceAddressForm(prefix='to',
                                                        instance=invoice_obj.to_address)
        return render(request, 'invoice_create_1.html', context)

    if request.method == 'POST':
        form = InvoiceForm(request.POST, instance=invoice_obj,
                           request_user=request.user)
        from_address_form = InvoiceAddressForm(
            request.POST, instance=invoice_obj.from_address, prefix='from')
        to_address_form = InvoiceAddressForm(
            request.POST, instance=invoice_obj.to_address, prefix='to')
        if form.is_valid() and from_address_form.is_valid() and to_address_form.is_valid():
            form_changed_data = form.changed_data
            form_changed_data.remove('from_address')
            form_changed_data.remove('to_address')
            form_changed_data = form_changed_data + ['from_' + field for field in from_address_form.changed_data]
            form_changed_data = form_changed_data + ['to_' + field for field in to_address_form.changed_data]
            # if form_changed_data:
            #     create_invoice_history(invoice_obj.id, request.user.id, form_changed_data)
            from_address_obj = from_address_form.save()
            to_address_obj = to_address_form.save()
            invoice_obj = form.save(commit=False)
            invoice_obj.created_by = request.user
            invoice_obj.from_address = from_address_obj
            invoice_obj.to_address = to_address_obj
            invoice_obj.save()
            form.save_m2m()
            if form_changed_data:
                create_invoice_history(invoice_obj.id, request.user.id, form_changed_data)

            if request.POST.getlist('teams', []):
                user_ids = Teams.objects.filter(id__in=request.POST.getlist('teams')).values_list('users', flat=True)
                assinged_to_users_ids = invoice_obj.assigned_to.all().values_list('id', flat=True)
                for user_id in user_ids:
                    if user_id not in assinged_to_users_ids:
                        invoice_obj.assigned_to.add(user_id)

            kwargs = {'domain': request.get_host(), 'protocol': request.scheme}
            send_email.delay(invoice_obj.id, **kwargs)
            if request.POST.get('from_account'):
                return JsonResponse({'error': False, 'success_url': reverse('accounts:view_account',
                    args=(request.POST.get('from_account'),))})
            return JsonResponse({'error': False, 'success_url': reverse('invoices:invoices_list')})
        else:
            return JsonResponse({'error': True, 'errors': form.errors,
                                 'from_address_errors': from_address_form.errors,
                                 'to_address_errors': to_address_form.errors})
Beispiel #2
0
def invoices_create(request):
    if request.method == 'GET':
        context = {}
        context["form"] = InvoiceForm(request_user=request.user)
        context["users"] = User.objects.all()
        # "prefix" use case in form, both from address and to address use the
        # same model and same model form, so the name attribute in the form will
        #  be same for the both forms and the address will be same for both
        # shipping and to address, so use prefix parameter to distinguish the
        # forms, don't remove this comment
        context["from_address_form"] = InvoiceAddressForm(
            prefix='from')
        context["to_address_form"] = InvoiceAddressForm(prefix='to')
        if request.user.role == 'ADMIN' or request.user.is_superuser:
            context['teams'] = Teams.objects.all()

        return render(request, 'invoice_create_1.html', context)

    if request.method == 'POST':
        form = InvoiceForm(request.POST, request_user=request.user)
        from_address_form = InvoiceAddressForm(
            request.POST, prefix='from')
        to_address_form = InvoiceAddressForm(request.POST, prefix='to')
        if form.is_valid() and from_address_form.is_valid() and to_address_form.is_valid():
            from_address_obj = from_address_form.save()
            to_address_obj = to_address_form.save()
            invoice_obj = form.save(commit=False)
            invoice_obj.created_by = request.user
            invoice_obj.from_address = from_address_obj
            invoice_obj.to_address = to_address_obj
            invoice_obj.save()
            form.save_m2m()

            if request.POST.getlist('teams', []):
                user_ids = Teams.objects.filter(id__in=request.POST.getlist('teams')).values_list('users', flat=True)
                assinged_to_users_ids = invoice_obj.assigned_to.all().values_list('id', flat=True)
                for user_id in user_ids:
                    if user_id not in assinged_to_users_ids:
                        invoice_obj.assigned_to.add(user_id)

            if request.POST.getlist('teams', []):
                invoice_obj.teams.add(*request.POST.getlist('teams'))

            create_invoice_history(invoice_obj.id, request.user.id, [])
            kwargs = {'domain': request.get_host(), 'protocol': request.scheme}
            assigned_to_list = list(invoice_obj.assigned_to.all().values_list('id', flat=True))
            send_email.delay(invoice_obj.id, assigned_to_list, **kwargs)
            if request.POST.get('from_account'):
                return JsonResponse({'error': False, 'success_url': reverse('accounts:view_account',
                    args=(request.POST.get('from_account'),))})
            return JsonResponse({'error': False, 'success_url': reverse('invoices:invoices_list')})
        else:
            return JsonResponse({'error': True, 'errors': form.errors,
                                 'from_address_errors': from_address_form.errors,
                                 'to_address_errors': to_address_form.errors})
Beispiel #3
0
def invoice_edit(request, invoice_id):
    invoice_obj = get_object_or_404(Invoice, pk=invoice_id)

    if not (request.user.role == 'ADMIN' or request.user.is_superuser
            or invoice_obj.created_by == request.user
            or request.user in invoice_obj.assigned_to.all()):
        raise PermissionDenied

    if request.method == 'GET':
        context = {}
        context['invoice_obj'] = invoice_obj
        context['form'] = InvoiceForm(instance=invoice_obj,
                                      request_user=request.user)
        context['from_address_form'] = InvoiceAddressForm(
            prefix='from', instance=invoice_obj.from_address)
        context['to_address_form'] = InvoiceAddressForm(
            prefix='to', instance=invoice_obj.to_address)
        return render(request, 'invoices_create.html', context)

    if request.method == 'POST':
        form = InvoiceForm(request.POST,
                           instance=invoice_obj,
                           request_user=request.user)
        from_address_form = InvoiceAddressForm(
            request.POST, instance=invoice_obj.from_address, prefix='from')
        to_address_form = InvoiceAddressForm(request.POST,
                                             instance=invoice_obj.to_address,
                                             prefix='to')
        if form.is_valid() and from_address_form.is_valid(
        ) and to_address_form.is_valid():
            from_address_obj = from_address_form.save()
            to_address_obj = to_address_form.save()
            invoice_obj = form.save(commit=False)
            invoice_obj.created_by = request.user
            invoice_obj.from_address = from_address_obj
            invoice_obj.to_address = to_address_obj
            invoice_obj.save()
            form.save_m2m()
            kwargs = {'domain': request.get_host(), 'protocol': request.scheme}
            send_email.delay(invoice_obj.id, **kwargs)
            return JsonResponse({
                'error':
                False,
                'success_url':
                reverse('invoices:invoices_list')
            })
        else:
            return JsonResponse({
                'error': True,
                'errors': form.errors,
                'from_address_errors': from_address_form.errors,
                'to_address_errors': to_address_form.errors
            })
Beispiel #4
0
def invoices_create(request):
    if request.method == 'GET':
        context = {}
        context["form"] = InvoiceForm(request_user=request.user)
        # "prefix" use case in form, both from address and to address use the
        # same model and same model form, so the name attribute in the form will
        #  be same for the both forms and the address will be same for both
        # shipping and to address, so use prefix parameter to distinguish the
        # forms, don't remove this comment
        context["from_address_form"] = InvoiceAddressForm(prefix='from')
        context["to_address_form"] = InvoiceAddressForm(prefix='to')

        return render(request, 'invoices_create.html', context)

    if request.method == 'POST':
        form = InvoiceForm(request.POST, request_user=request.user)
        from_address_form = InvoiceAddressForm(request.POST, prefix='from')
        to_address_form = InvoiceAddressForm(request.POST, prefix='to')
        if form.is_valid() and from_address_form.is_valid(
        ) and to_address_form.is_valid():
            from_address_obj = from_address_form.save()
            to_address_obj = to_address_form.save()
            invoice_obj = form.save(commit=False)
            invoice_obj.created_by = request.user
            invoice_obj.from_address = from_address_obj
            invoice_obj.to_address = to_address_obj
            invoice_obj.save()
            form.save_m2m()
            kwargs = {'domain': request.get_host(), 'protocol': request.scheme}
            send_email.delay(invoice_obj.id, **kwargs)
            return JsonResponse({
                'error':
                False,
                'success_url':
                reverse('invoices:invoices_list')
            })
        else:
            return JsonResponse({
                'error': True,
                'errors': form.errors,
                'from_address_errors': from_address_form.errors,
                'to_address_errors': to_address_form.errors
            })
Beispiel #5
0
def invoice_edit(request, invoice_id):
    invoice_obj = get_object_or_404(Invoice, pk=invoice_id)

    if invoice_obj.company != request.company:
        raise PermissionDenied

    if not (request.user.role == "ADMIN" or request.user.is_superuser
            or invoice_obj.created_by == request.user
            or request.user in invoice_obj.assigned_to.all()):
        raise PermissionDenied

    if request.method == "GET":
        context = {}
        context["invoice_obj"] = invoice_obj
        context["teams"] = Teams.objects.filter(company=request.company)
        context["users"] = User.objects.filter(company=request.company)
        context["form"] = InvoiceForm(instance=invoice_obj,
                                      request_user=request.user,
                                      request_obj=request)
        context["from_address_form"] = InvoiceAddressForm(
            prefix="from", instance=invoice_obj.from_address)
        context["to_address_form"] = InvoiceAddressForm(
            prefix="to", instance=invoice_obj.to_address)
        return render(request, "invoice_create_1.html", context)

    if request.method == "POST":
        form = InvoiceForm(
            request.POST,
            instance=invoice_obj,
            request_user=request.user,
            request_obj=request,
        )
        from_address_form = InvoiceAddressForm(
            request.POST, instance=invoice_obj.from_address, prefix="from")
        to_address_form = InvoiceAddressForm(request.POST,
                                             instance=invoice_obj.to_address,
                                             prefix="to")
        previous_assigned_to_users = list(
            invoice_obj.assigned_to.all().values_list("id", flat=True))
        if (form.is_valid() and from_address_form.is_valid()
                and to_address_form.is_valid()):
            form_changed_data = form.changed_data
            form_changed_data.remove("from_address")
            form_changed_data.remove("to_address")
            form_changed_data = form_changed_data + [
                "from_" + field for field in from_address_form.changed_data
            ]
            form_changed_data = form_changed_data + [
                "to_" + field for field in to_address_form.changed_data
            ]
            # if form_changed_data:
            #     create_invoice_history(invoice_obj.id, request.user.id, form_changed_data)
            from_address_obj = from_address_form.save()
            to_address_obj = to_address_form.save()
            invoice_obj = form.save(commit=False)
            invoice_obj.created_by = request.user
            invoice_obj.from_address = from_address_obj
            invoice_obj.to_address = to_address_obj
            invoice_obj.save()
            form.save_m2m()
            if form_changed_data:
                create_invoice_history(invoice_obj.id, request.user.id,
                                       form_changed_data)

            if request.POST.getlist("teams", []):
                user_ids = Teams.objects.filter(
                    id__in=request.POST.getlist("teams")).values_list(
                        "users", flat=True)
                assinged_to_users_ids = invoice_obj.assigned_to.all(
                ).values_list("id", flat=True)
                for user_id in user_ids:
                    if user_id not in assinged_to_users_ids:
                        invoice_obj.assigned_to.add(user_id)

            if request.POST.getlist("teams", []):
                invoice_obj.teams.clear()
                invoice_obj.teams.add(*request.POST.getlist("teams"))
            else:
                invoice_obj.teams.clear()

            kwargs = {"domain": request.get_host(), "protocol": request.scheme}

            assigned_to_list = list(invoice_obj.assigned_to.all().values_list(
                "id", flat=True))
            recipients = list(
                set(assigned_to_list) - set(previous_assigned_to_users))
            # send_email_to_assigned_user.delay(recipients, account_object.id, domain=current_site.domain,
            #     protocol=self.request.scheme)

            send_email.delay(invoice_obj.id, recipients, **kwargs)
            if request.POST.get("from_account"):
                return JsonResponse({
                    "error":
                    False,
                    "success_url":
                    reverse(
                        "accounts:view_account",
                        args=(request.POST.get("from_account"), ),
                    ),
                })
            return JsonResponse({
                "error":
                False,
                "success_url":
                reverse("invoices:invoices_list")
            })
        else:
            return JsonResponse({
                "error": True,
                "errors": form.errors,
                "from_address_errors": from_address_form.errors,
                "to_address_errors": to_address_form.errors,
            })
Beispiel #6
0
def invoices_create(request):
    if request.method == "GET":
        context = {}
        context["form"] = InvoiceForm(request_user=request.user,
                                      request_obj=request)
        context["users"] = User.objects.filter(company=request.company)
        # "prefix" use case in form, both from address and to address use the
        # same model and same model form, so the name attribute in the form will
        #  be same for the both forms and the address will be same for both
        # shipping and to address, so use prefix parameter to distinguish the
        # forms, don't remove this comment
        context["from_address_form"] = InvoiceAddressForm(prefix="from")
        context["to_address_form"] = InvoiceAddressForm(prefix="to")
        if request.user.role == "ADMIN" or request.user.is_superuser:
            context["teams"] = Teams.objects.filter(company=request.company)

        return render(request, "invoice_create_1.html", context)

    if request.method == "POST":
        form = InvoiceForm(request.POST,
                           request_user=request.user,
                           request_obj=request)
        from_address_form = InvoiceAddressForm(request.POST, prefix="from")
        to_address_form = InvoiceAddressForm(request.POST, prefix="to")
        if (form.is_valid() and from_address_form.is_valid()
                and to_address_form.is_valid()):
            from_address_obj = from_address_form.save()
            to_address_obj = to_address_form.save()
            invoice_obj = form.save(commit=False)
            invoice_obj.created_by = request.user
            invoice_obj.company = request.company
            invoice_obj.from_address = from_address_obj
            invoice_obj.to_address = to_address_obj
            invoice_obj.save()
            form.save_m2m()

            if request.POST.getlist("teams", []):
                user_ids = Teams.objects.filter(
                    id__in=request.POST.getlist("teams")).values_list(
                        "users", flat=True)
                assinged_to_users_ids = invoice_obj.assigned_to.all(
                ).values_list("id", flat=True)
                for user_id in user_ids:
                    if user_id not in assinged_to_users_ids:
                        invoice_obj.assigned_to.add(user_id)

            if request.POST.getlist("teams", []):
                invoice_obj.teams.add(*request.POST.getlist("teams"))

            create_invoice_history(invoice_obj.id, request.user.id, [])
            kwargs = {"domain": request.get_host(), "protocol": request.scheme}
            assigned_to_list = list(invoice_obj.assigned_to.all().values_list(
                "id", flat=True))
            send_email.delay(invoice_obj.id, assigned_to_list, **kwargs)
            if request.POST.get("from_account"):
                return JsonResponse({
                    "error":
                    False,
                    "success_url":
                    reverse(
                        "accounts:view_account",
                        args=(request.POST.get("from_account"), ),
                    ),
                })
            return JsonResponse({
                "error":
                False,
                "success_url":
                reverse("invoices:invoices_list")
            })
        else:
            return JsonResponse({
                "error": True,
                "errors": form.errors,
                "from_address_errors": from_address_form.errors,
                "to_address_errors": to_address_form.errors,
            })