update
This commit is contained in:
commit
f7a43a3b22
@ -1,3 +1,6 @@
|
|||||||
|
from django.shortcuts import redirect
|
||||||
|
from django.contrib import messages
|
||||||
|
from django.utils import timezone
|
||||||
import requests
|
import requests
|
||||||
from inventory import models
|
from inventory import models
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -68,3 +71,23 @@ def get_user_type(request):
|
|||||||
dealer = request.user.staff.dealer
|
dealer = request.user.staff.dealer
|
||||||
return dealer
|
return dealer
|
||||||
|
|
||||||
|
def get_dealer_from_instance(instance):
|
||||||
|
if instance.dealer.staff:
|
||||||
|
return instance.dealer
|
||||||
|
else:
|
||||||
|
return instance.dealer
|
||||||
|
|
||||||
|
|
||||||
|
def reserve_car(car,request):
|
||||||
|
try:
|
||||||
|
reserved_until = timezone.now() + timezone.timedelta(hours=24)
|
||||||
|
models.CarReservation.objects.create(
|
||||||
|
car=car, reserved_by=request.user, reserved_until=reserved_until
|
||||||
|
)
|
||||||
|
car.status = models.CarStatusChoices.RESERVED
|
||||||
|
car.save()
|
||||||
|
messages.success(request, _("Car reserved successfully."))
|
||||||
|
except Exception as e:
|
||||||
|
messages.error(request, f"Error reserving car: {e}")
|
||||||
|
|
||||||
|
return redirect("car_detail", pk=car.pk)
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
import requests
|
||||||
|
from django.test import Client
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
@ -67,7 +69,7 @@ from . import models, forms
|
|||||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||||
from django.contrib.messages.views import SuccessMessageMixin
|
from django.contrib.messages.views import SuccessMessageMixin
|
||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from .utils import get_calculations, send_email, get_user_type
|
from .utils import get_calculations, reserve_car, send_email, get_user_type
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from allauth.account import views
|
from allauth.account import views
|
||||||
from django.db.models import Count, F, Value
|
from django.db.models import Count, F, Value
|
||||||
@ -637,23 +639,12 @@ class CustomCardCreateView(LoginRequiredMixin, CreateView):
|
|||||||
@login_required()
|
@login_required()
|
||||||
def reserve_car_view(request, car_id):
|
def reserve_car_view(request, car_id):
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
car = get_object_or_404(models.Car, pk=car_id)
|
car = get_object_or_404(models.Car, pk=car_id)
|
||||||
if car.is_reserved():
|
if car.is_reserved():
|
||||||
messages.error(request, _("This car is already reserved."))
|
messages.error(request, _("This car is already reserved."))
|
||||||
return redirect("car_detail", pk=car.pk)
|
return redirect("car_detail", pk=car.pk)
|
||||||
|
response = reserve_car(car, request)
|
||||||
try:
|
return response
|
||||||
reserved_until = timezone.now() + timezone.timedelta(hours=24)
|
|
||||||
models.CarReservation.objects.create(
|
|
||||||
car=car, reserved_by=request.user, reserved_until=reserved_until
|
|
||||||
)
|
|
||||||
car.status = models.CarStatusChoices.RESERVED
|
|
||||||
car.save()
|
|
||||||
messages.success(request, _("Car reserved successfully."))
|
|
||||||
except Exception as e:
|
|
||||||
messages.error(request, f"Error reserving car: {e}")
|
|
||||||
|
|
||||||
return redirect("car_detail", pk=car.pk)
|
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{"success": False, "message": "Invalid request method."}, status=400
|
{"success": False, "message": "Invalid request method."}, status=400
|
||||||
)
|
)
|
||||||
@ -1760,42 +1751,36 @@ def create_estimate(request):
|
|||||||
|
|
||||||
items = data.get("item", [])
|
items = data.get("item", [])
|
||||||
quantities = data.get("quantity", [])
|
quantities = data.get("quantity", [])
|
||||||
|
|
||||||
|
|
||||||
if not all([items, quantities]):
|
if not all([items, quantities]):
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{"status": "error", "message": "Items and Quantities are required"},
|
{"status": "error", "message": "Items and Quantities are required"},
|
||||||
status=400,
|
status=400,
|
||||||
)
|
)
|
||||||
if isinstance(quantities, list):
|
if isinstance(quantities, list):
|
||||||
if '0' in quantities:
|
if "0" in quantities:
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{
|
{"status": "error", "message": "Quantity must be greater than zero"}
|
||||||
"status": "error",
|
)
|
||||||
"message": "Quantity must be greater than zero"
|
|
||||||
})
|
|
||||||
else:
|
else:
|
||||||
if int(quantities) <= 0:
|
if int(quantities) <= 0:
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{
|
{"status": "error", "message": "Quantity must be greater than zero"}
|
||||||
"status": "error",
|
)
|
||||||
"message": "Quantity must be greater than zero"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
estimate = entity.create_estimate(
|
estimate = entity.create_estimate(
|
||||||
estimate_title=title, customer_model=customer, contract_terms=terms
|
estimate_title=title, customer_model=customer, contract_terms=terms
|
||||||
)
|
)
|
||||||
if isinstance(items, list):
|
if isinstance(items, list):
|
||||||
item_quantity_map = {}
|
item_quantity_map = {}
|
||||||
for item, quantity in zip(items, quantities):
|
for item, quantity in zip(items, quantities):
|
||||||
if item in item_quantity_map:
|
if item in item_quantity_map:
|
||||||
item_quantity_map[item] += int(quantity)
|
item_quantity_map[item] += int(quantity)
|
||||||
else:
|
else:
|
||||||
item_quantity_map[item] = int(quantity)
|
item_quantity_map[item] = int(quantity)
|
||||||
item_list = list(item_quantity_map.keys())
|
item_list = list(item_quantity_map.keys())
|
||||||
quantity_list = list(item_quantity_map.values())
|
quantity_list = list(item_quantity_map.values())
|
||||||
|
|
||||||
items_list = [
|
items_list = [
|
||||||
{"item_id": item_list[i], "quantity": quantity_list[i]}
|
{"item_id": item_list[i], "quantity": quantity_list[i]}
|
||||||
for i in range(len(item_list))
|
for i in range(len(item_list))
|
||||||
@ -1845,22 +1830,22 @@ def create_estimate(request):
|
|||||||
if isinstance(items, list):
|
if isinstance(items, list):
|
||||||
for item in items:
|
for item in items:
|
||||||
item_instance = ItemModel.objects.get(pk=item)
|
item_instance = ItemModel.objects.get(pk=item)
|
||||||
instance = models.Car.objects.get(vin=item_instance.name)
|
instance = models.Car.objects.get(vin=item_instance.name)
|
||||||
instance.status = models.CarStatusChoices.RESERVED
|
response = reserve_car(instance, request)
|
||||||
instance.save()
|
|
||||||
else:
|
else:
|
||||||
item_instance = ItemModel.objects.get(pk=items)
|
item_instance = ItemModel.objects.get(pk=items)
|
||||||
instance = models.Car.objects.get(vin=item_instance.name)
|
instance = models.Car.objects.get(vin=item_instance.name)
|
||||||
instance.status = models.CarStatusChoices.RESERVED
|
response = reserve_car(instance, request)
|
||||||
instance.save()
|
|
||||||
|
|
||||||
url = reverse("estimate_detail", kwargs={"pk": estimate.pk})
|
url = reverse("estimate_detail", kwargs={"pk": estimate.pk})
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{
|
{
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"message": "Estimate created successfully!",
|
"message": "Estimate created successfully!",
|
||||||
"url": f"{url}"
|
"url": f"{url}",
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# except Exception as e:
|
# except Exception as e:
|
||||||
# return JsonResponse(
|
# return JsonResponse(
|
||||||
@ -1899,13 +1884,21 @@ class EstimateDetailView(LoginRequiredMixin, DetailView):
|
|||||||
estimate = kwargs.get("object")
|
estimate = kwargs.get("object")
|
||||||
if estimate.get_itemtxs_data():
|
if estimate.get_itemtxs_data():
|
||||||
total = sum(
|
total = sum(
|
||||||
(x.ce_revenue_estimate - models.Car.objects.get(vin=x.item_model.name).finances.discount_amount) for x in estimate.get_itemtxs_data()[0].all()
|
(
|
||||||
|
x.ce_revenue_estimate
|
||||||
|
- models.Car.objects.get(
|
||||||
|
vin=x.item_model.name
|
||||||
|
).finances.discount_amount
|
||||||
|
)
|
||||||
|
for x in estimate.get_itemtxs_data()[0].all()
|
||||||
)
|
)
|
||||||
vat = models.VatRate.objects.filter(is_active=True).first()
|
vat = models.VatRate.objects.filter(is_active=True).first()
|
||||||
|
|
||||||
# Calculate VAT and total with 2 decimal places
|
# Calculate VAT and total with 2 decimal places
|
||||||
vat_amount = round(total * vat.vat_rate, 2) # Round to 2 decimal places
|
vat_amount = round(total * vat.vat_rate, 2) # Round to 2 decimal places
|
||||||
grand_total = round((total * vat.vat_rate) + total, 2) # Round to 2 decimal places
|
grand_total = round(
|
||||||
|
(total * vat.vat_rate) + total, 2
|
||||||
|
) # Round to 2 decimal places
|
||||||
|
|
||||||
# Add values to the context
|
# Add values to the context
|
||||||
kwargs["vat_amount"] = vat_amount
|
kwargs["vat_amount"] = vat_amount
|
||||||
@ -1958,29 +1951,23 @@ def estimate_mark_as(request, pk):
|
|||||||
if mark == "review":
|
if mark == "review":
|
||||||
if not estimate.can_review():
|
if not estimate.can_review():
|
||||||
messages.error(request, "Estimate is not ready for review")
|
messages.error(request, "Estimate is not ready for review")
|
||||||
return redirect("estimate_detail", pk=estimate.pk)
|
return redirect("estimate_detail", pk=estimate.pk)
|
||||||
estimate.mark_as_review()
|
estimate.mark_as_review()
|
||||||
|
|
||||||
elif mark == "approved":
|
elif mark == "approved":
|
||||||
if not estimate.can_approve():
|
if not estimate.can_approve():
|
||||||
messages.error(request, "Estimate is not ready for approval")
|
messages.error(request, "Estimate is not ready for approval")
|
||||||
return redirect("estimate_detail", pk=estimate.pk)
|
return redirect("estimate_detail", pk=estimate.pk)
|
||||||
estimate.mark_as_approved()
|
estimate.mark_as_approved()
|
||||||
messages.success(request, "Estimate approved successfully.")
|
|
||||||
for i in estimate.get_itemtxs_data()[0]:
|
messages.success(request, "Estimate approved successfully.")
|
||||||
car = models.Car.objects.get(vin=i.item_model.name)
|
|
||||||
car.status = models.CarStatusChoices.SOLD
|
|
||||||
car.save()
|
|
||||||
elif mark == "rejected":
|
elif mark == "rejected":
|
||||||
if not estimate.can_cancel():
|
if not estimate.can_cancel():
|
||||||
messages.error(request, "Estimate is not ready for rejection")
|
messages.error(request, "Estimate is not ready for rejection")
|
||||||
return redirect("estimate_detail", pk=estimate.pk)
|
return redirect("estimate_detail", pk=estimate.pk)
|
||||||
estimate.mark_as_canceled()
|
estimate.mark_as_canceled()
|
||||||
messages.success(request, "Estimate canceled successfully.")
|
messages.success(request, "Estimate canceled successfully.")
|
||||||
for i in estimate.get_itemtxs_data()[0]:
|
|
||||||
car = models.Car.objects.get(vin=i.item_model.name)
|
|
||||||
car.status = models.CarStatusChoices.AVAILABLE
|
|
||||||
car.save()
|
|
||||||
|
|
||||||
elif mark == "completed":
|
elif mark == "completed":
|
||||||
if not estimate.can_complete():
|
if not estimate.can_complete():
|
||||||
messages.error(request, "Estimate is not ready for completion")
|
messages.error(request, "Estimate is not ready for completion")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user