add validation for car finance and estimate discount

This commit is contained in:
ismail 2025-08-06 13:14:13 +03:00
parent bd1e4d73ae
commit 4cc61a6fa3
5 changed files with 48 additions and 36 deletions

View File

@ -432,25 +432,20 @@ class CarFinanceForm(forms.ModelForm):
additional services associated with a car finance application.
"""
# additional_finances = forms.ModelMultipleChoiceField(
# queryset=AdditionalServices.objects.all(),
# widget=forms.CheckboxSelectMultiple(attrs={"class": "form-check-input"}),
# required=False,
# )
def clean(self):
cleaned_data = super().clean()
cost_price = cleaned_data.get("cost_price")
marked_price = cleaned_data.get("marked_price")
if cost_price > marked_price:
raise forms.ValidationError({"cost_price": "Cost price should not be greater than marked price"})
return cleaned_data
class Meta:
model = CarFinance
fields = ["cost_price","marked_price"]
# def save(self, commit=True):
# instance = super().save()
# try:
# instance.additional_services.set(self.cleaned_data["additional_finances"])
# except KeyError:
# pass
# instance.save()
# return instance
class CarLocationForm(forms.ModelForm):
"""
@ -1172,6 +1167,7 @@ class ScheduleForm(forms.ModelForm):
scheduled_at = forms.DateTimeField(
widget=DateTimeInput(attrs={"type": "datetime-local"})
)
reminder = forms.BooleanField(help_text=_("Send a reminder?"),required=False)
class Meta:
model = Schedule

View File

@ -4912,10 +4912,19 @@ def update_estimate_discount(request, dealer_slug, pk):
object_id=estimate.pk,
)
discount_amount = request.POST.get("discount_amount", 0)
calculator = CarFinanceCalculator(estimate)
finance_data = calculator.get_finance_data()
if Decimal(discount_amount) >= finance_data.get('cars')[0]['marked_price']:
messages.error(request, _("Discount amount cannot be greater than marked price"))
return redirect("estimate_detail", dealer_slug=dealer_slug, pk=pk)
print(finance_data.get('cars')[0]['marked_price'] * Decimal('0.5'))
if Decimal(discount_amount) > finance_data.get('cars')[0]['marked_price'] * Decimal('0.5'):
messages.warning(request, _("Discount amount is greater than 50% of the marked price, proceed with caution."))
else:
messages.success(request, _("Discount updated successfully"))
extra_info.data.update({"discount": Decimal(discount_amount)})
extra_info.save()
messages.success(request, "Discount updated successfully")
return redirect("estimate_detail", dealer_slug=dealer_slug, pk=pk)
@ -6535,6 +6544,7 @@ def schedule_event(request, dealer_slug, content_type, slug):
form = forms.ScheduleForm(request.POST)
if form.is_valid():
reminder = form.cleaned_data['reminder']
instance = form.save(commit=False)
instance.dealer = dealer
instance.content_object = obj
@ -6581,6 +6591,7 @@ def schedule_event(request, dealer_slug, content_type, slug):
created_by=request.user,
activity_type=instance.scheduled_type,
)
if reminder:
scheduled_at_aware = timezone.make_aware(instance.scheduled_at, timezone.get_current_timezone()) if timezone.is_naive(instance.scheduled_at) else instance.scheduled_at
reminder_time = scheduled_at_aware - timezone.timedelta(minutes=15)
@ -9689,10 +9700,10 @@ def update_task(request, dealer_slug, pk):
@permission_required("inventory.change_schedule", raise_exception=True)
def update_schedule(request, dealer_slug, pk):
task = get_object_or_404(models.Schedule, pk=pk)
if request.method == "POST":
task.completed = False if task.completed else True
task.save()
print("task")
return render(request, "partials/task.html", {"task": task})

View File

@ -138,7 +138,8 @@ html[dir="rtl"] .form-icon-container .form-control {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 10;
z-index: 9999;
pointer-events: none;
}
#spinner-bg {
@ -150,11 +151,14 @@ html[dir="rtl"] .form-icon-container .form-control {
background-color: rgba(255, 255, 255, 0.7);
opacity: 0;
transition: opacity 500ms ease-in;
z-index: 5;
visibility: hidden;
z-index: 10000;
pointer-events: none;
}
#spinner-bg.htmx-request {
opacity: .8;
visibility: visible;
}

View File

@ -84,10 +84,10 @@
{% include "plans/expiration_messages.html" %}
{% block period_navigation %}
{% endblock period_navigation %}
<div id="main_content" class="fade-me-in" hx-boost="false" hx-target="#main_content" hx-select="#main_content" hx-swap="outerHTML transition:true" hx-select-oob="#toast-container" hx-history-elt>
<div id="spinner" class="htmx-indicator spinner-bg">
<img src="{% static 'spinner.svg' %}" width="100" height="100" alt="">
</div>
<div id="main_content" class="fade-me-in" hx-boost="false" hx-target="#main_content" hx-select="#main_content" hx-swap="outerHTML transition:true" hx-select-oob="#toast-container" hx-history-elt>
{% block customCSS %}{% endblock %}
{% block content %}{% endblock content %}
{% block customJS %}{% endblock %}

View File

@ -9,7 +9,8 @@
type="checkbox"
hx-post="{% url 'update_schedule' request.dealer.slug task.pk %}"
hx-trigger="change"
hx-swap="outerHTML"
hx-swap="none"
hx-on:click="$(this).closest('tr').toggleClass('completed-task')"
hx-target="#task-{{ task.pk }}" />
</div>
</td>
@ -18,7 +19,7 @@
<div class="fs-10 d-block">{{ task.scheduled_type|capfirst }}</div>
</td>
<td class="sent align-middle white-space-nowrap text-start fw-thin text-body-tertiary py-2">{{ task.notes }}</td>
<td class="date align-middle white-space-nowrap text-body py-2">{{ task.created_at|naturalday|capfirst }}</td>
<td class="date align-middle white-space-nowrap text-body py-2">{{ task.scheduled_at|naturaltime|capfirst }}</td>
<td class="date align-middle white-space-nowrap text-body py-2">
{% if task.completed %}
<span class="badge badge-phoenix fs-10 badge-phoenix-success"><i class="fa-solid fa-check"></i></span>