save
This commit is contained in:
parent
286265ac59
commit
8e6e87a098
@ -33,7 +33,7 @@ from .models import (
|
||||
Staff,
|
||||
Opportunity, Priority, Sources, Lead, Activity, Notes, CarModel
|
||||
)
|
||||
from django_ledger.models import ItemModel, InvoiceModel
|
||||
from django_ledger.models import ItemModel, InvoiceModel,BillModel
|
||||
from django.forms import ModelMultipleChoiceField, ValidationError, DateInput
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
import django_tables2 as tables
|
||||
@ -553,7 +553,10 @@ class ItemForm(forms.Form):
|
||||
|
||||
class PaymentForm(forms.Form):
|
||||
invoice = forms.ModelChoiceField(
|
||||
queryset=InvoiceModel.objects.all(), label="Invoice", required=True
|
||||
queryset=InvoiceModel.objects.all(), label="Invoice", required=False
|
||||
)
|
||||
bill = forms.ModelChoiceField(
|
||||
queryset=BillModel.objects.all(), label="Bill", required=False
|
||||
)
|
||||
amount = forms.DecimalField(label="Amount", required=True)
|
||||
payment_method = forms.ChoiceField(
|
||||
@ -571,17 +574,19 @@ class PaymentForm(forms.Form):
|
||||
|
||||
def clean_amount(self):
|
||||
invoice = self.cleaned_data['invoice']
|
||||
bill = self.cleaned_data['bill']
|
||||
model = invoice if invoice else bill
|
||||
amount = self.cleaned_data['amount']
|
||||
if amount < invoice.amount_due:
|
||||
raise forms.ValidationError("Payment amount is greater than invoice amount due")
|
||||
if amount + model.amount_paid > model.amount_due:
|
||||
raise forms.ValidationError("Payment amount is greater than amount due")
|
||||
if amount <= 0:
|
||||
raise forms.ValidationError("Payment amount must be greater than 0")
|
||||
if invoice.amount_due == invoice.amount_paid or invoice.invoice_status == "paid":
|
||||
if model.is_paid():
|
||||
raise forms.ValidationError("Invoice is already paid")
|
||||
if amount > invoice.amount_due:
|
||||
raise forms.ValidationError("Payment amount is greater than invoice amount due")
|
||||
return amount
|
||||
|
||||
if amount > model.amount_due:
|
||||
raise forms.ValidationError("Payment amount is greater than amount due")
|
||||
return amount
|
||||
|
||||
|
||||
|
||||
class EmailForm(forms.Form):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user