diff --git a/reference_documents/forms/preferential_quota_forms.py b/reference_documents/forms/preferential_quota_forms.py index af23f8a5e..da10dd2df 100644 --- a/reference_documents/forms/preferential_quota_forms.py +++ b/reference_documents/forms/preferential_quota_forms.py @@ -13,7 +13,6 @@ from reference_documents.models import PreferentialQuota from reference_documents.models import PreferentialQuotaOrderNumber from reference_documents.validators import commodity_code_validator -from reference_documents.validators import order_number_validator class PreferentialQuotaCreateUpdateForm( @@ -31,6 +30,31 @@ class Meta: "valid_between", ] + def __init__(self, reference_document_version, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields[ + "quota_order_number" + ].queryset = reference_document_version.preferential_quota_order_numbers.all() + self.reference_document_version = reference_document_version + self.helper = FormHelper(self) + self.helper.label_size = Size.SMALL + self.helper.legend_size = Size.SMALL + self.helper.layout = Layout( + "quota_order_number", + "commodity_code", + "quota_duty_rate", + "volume", + "measurement", + "start_date", + "end_date", + Submit( + "submit", + "Save", + data_module="govuk-button", + data_prevent_double_click="true", + ), + ) + commodity_code = forms.CharField( help_text="Commodity Code", validators=[commodity_code_validator], @@ -49,13 +73,16 @@ class Meta: }, ) - quota_order_number = forms.CharField( - help_text="Quota Order Number", - validators=[order_number_validator], + quota_order_number = forms.ModelChoiceField( + label="Quota Order Number", + help_text="Select Quota order number", + queryset=PreferentialQuotaOrderNumber.objects.all(), + validators=[], error_messages={ - "invalid": "Quota Order Number is invalid", - "required": "Quota Order Number is required", + "invalid": "Quota Order number is invalid", }, + required=False, + widget=forms.Select(attrs={"class": "form-control"}), ) volume = forms.CharField( @@ -82,27 +109,6 @@ def clean_quota_duty_rate(self): raise ValidationError("Quota duty Rate is not valid - it must have a value") return data - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.helper = FormHelper(self) - self.helper.label_size = Size.SMALL - self.helper.legend_size = Size.SMALL - self.helper.layout = Layout( - "quota_order_number", - "commodity_code", - "quota_duty_rate", - "volume", - "measurement", - "start_date", - "end_date", - Submit( - "submit", - "Save", - data_module="govuk-button", - data_prevent_double_click="true", - ), - ) - class PreferentialQuotaBulkCreate(ValidityPeriodForm, forms.ModelForm): commodity_codes = forms.CharField( diff --git a/reference_documents/forms/preferential_quota_order_number_forms.py b/reference_documents/forms/preferential_quota_order_number_forms.py index f4cee9abe..a92218c05 100644 --- a/reference_documents/forms/preferential_quota_order_number_forms.py +++ b/reference_documents/forms/preferential_quota_order_number_forms.py @@ -4,6 +4,7 @@ from crispy_forms_gds.layout import Size from crispy_forms_gds.layout import Submit from django import forms +from django.core.exceptions import ValidationError from common.forms import ValidityPeriodForm from reference_documents.models import PreferentialQuotaOrderNumber @@ -27,6 +28,7 @@ def __init__(self, reference_document_version, *args, **kwargs): self.fields[ "main_order_number" ].queryset = reference_document_version.preferential_quota_order_numbers.all() + self.reference_document_version = reference_document_version self.helper = FormHelper(self) self.helper.label_size = Size.SMALL self.helper.legend_size = Size.SMALL @@ -46,6 +48,29 @@ def __init__(self, reference_document_version, *args, **kwargs): ), ) + def clean(self): + cleaned_data = super().clean() + coefficient = cleaned_data.get("coefficient") + main_order_number = cleaned_data.get("main_order_number") + + # cant have one without the other + if coefficient and not main_order_number: + raise ValidationError( + "Coefficient specified without main order number", + ) + elif not coefficient and main_order_number: + raise ValidationError( + "Main order number specified without coefficient", + ) + + def clean_quota_order_number(self): + data = self.cleaned_data["quota_order_number"] + if self.reference_document_version.preferential_quota_order_numbers.filter( + quota_order_number=data, + ).exists(): + raise ValidationError("Quota Order Number Already Exists") + return data + quota_order_number = forms.CharField( label="Order number", help_text="Enter a six digit number", diff --git a/reference_documents/jinja2/includes/tabs/preferential_quotas.jinja b/reference_documents/jinja2/includes/tabs/preferential_quotas.jinja index e131a7948..80c1baa89 100644 --- a/reference_documents/jinja2/includes/tabs/preferential_quotas.jinja +++ b/reference_documents/jinja2/includes/tabs/preferential_quotas.jinja @@ -21,10 +21,26 @@ {% else %}

Order Number {{ value["quota_order_number_text"] }}

{% endif %} - {{ govukTable({ - "head": reference_document_version_quotas_headers, - "rows": value['data_rows'] - }) }} +
+ Valid between: {{ value['ref_doc_order_number'].valid_between }} + {% if value['ref_doc_order_number'].main_order_number %} + Sub Quota to {{ value['ref_doc_order_number'].main_order_number.quota_order_number }} + {% endif %} +
+
+ Edit + Delete +
+ {% if value['data_rows'] != [] %} + {{ govukTable({ + "head": reference_document_version_quotas_headers, + "rows": value['data_rows'] + }) }} + {% else %} +
+ No Quota definitions defined +
+ {% endif %} {% endfor %} diff --git a/reference_documents/views/preferential_quota_order_number_views.py b/reference_documents/views/preferential_quota_order_number_views.py index 997cf2468..5391a76a3 100644 --- a/reference_documents/views/preferential_quota_order_number_views.py +++ b/reference_documents/views/preferential_quota_order_number_views.py @@ -1,23 +1,27 @@ from django.contrib.auth.mixins import PermissionRequiredMixin from django.urls import reverse from django.views.generic import CreateView +from django.views.generic import DeleteView from django.views.generic import UpdateView from reference_documents.forms.preferential_quota_order_number_forms import ( PreferentialQuotaOrderNumberCreateUpdateForm, ) -from reference_documents.models import PreferentialQuota +from reference_documents.models import PreferentialQuotaOrderNumber from reference_documents.models import ReferenceDocumentVersion class PreferentialQuotaOrderNumberEditView(PermissionRequiredMixin, UpdateView): template_name = "reference_documents/preferential_quota_order_numbers/edit.jinja" permission_required = "reference_documents.edit_reference_document" - model = PreferentialQuota + model = PreferentialQuotaOrderNumber form_class = PreferentialQuotaOrderNumberCreateUpdateForm def get_form_kwargs(self): kwargs = super(PreferentialQuotaOrderNumberEditView, self).get_form_kwargs() + kwargs["reference_document_version"] = PreferentialQuotaOrderNumber.objects.get( + id=self.kwargs["pk"], + ).reference_document_version return kwargs def get_success_url(self): @@ -30,7 +34,7 @@ def get_success_url(self): class PreferentialQuotaOrderNumberCreateView(PermissionRequiredMixin, CreateView): template_name = "reference_documents/preferential_quota_order_numbers/edit.jinja" permission_required = "reference_documents.edit_reference_document" - model = PreferentialQuota + model = PreferentialQuotaOrderNumber form_class = PreferentialQuotaOrderNumberCreateUpdateForm def get_form_kwargs(self): @@ -53,7 +57,7 @@ def form_valid(self, form): def get_success_url(self): return ( reverse( - "reference_documents:version_details", + "reference_documents:version-details", args=[self.object.reference_document_version.pk], ) + "#tariff-quotas" @@ -71,7 +75,7 @@ def get_success_url(self): # ) -class PreferentialQuotaOrderNumberDeleteView(PermissionRequiredMixin, UpdateView): - template_name = "preferential_quota_order_numbers/delete.jinja" +class PreferentialQuotaOrderNumberDeleteView(PermissionRequiredMixin, DeleteView): + template_name = "reference_documents/preferential_quota_order_numbers/delete.jinja" permission_required = "reference_documents.edit_reference_document" - model = PreferentialQuota + model = PreferentialQuotaOrderNumber diff --git a/reference_documents/views/preferential_quota_views.py b/reference_documents/views/preferential_quota_views.py index c99f1691a..398dde170 100644 --- a/reference_documents/views/preferential_quota_views.py +++ b/reference_documents/views/preferential_quota_views.py @@ -15,6 +15,7 @@ PreferentialQuotaCreateUpdateForm, ) from reference_documents.models import PreferentialQuota +from reference_documents.models import PreferentialQuotaOrderNumber from reference_documents.models import ReferenceDocumentVersion @@ -24,6 +25,13 @@ class PreferentialQuotaEditView(PermissionRequiredMixin, UpdateView): model = PreferentialQuota form_class = PreferentialQuotaCreateUpdateForm + def get_form_kwargs(self): + kwargs = super(PreferentialQuotaEditView, self).get_form_kwargs() + kwargs["reference_document_version"] = PreferentialQuotaOrderNumber.objects.get( + id=self.kwargs["pk"], + ).reference_document_version + return kwargs + def post(self, request, *args, **kwargs): quota = self.get_object() quota.save() @@ -42,6 +50,13 @@ class PreferentialQuotaCreateView(PermissionRequiredMixin, CreateView): model = PreferentialQuota form_class = PreferentialQuotaCreateUpdateForm + def get_form_kwargs(self): + kwargs = super(PreferentialQuotaCreateView, self).get_form_kwargs() + kwargs["reference_document_version"] = ReferenceDocumentVersion.objects.get( + id=self.kwargs["pk"], + ) + return kwargs + def form_valid(self, form): instance = form.instance reference_document_version = ReferenceDocumentVersion.objects.get( @@ -61,17 +76,6 @@ def get_success_url(self): + "#tariff-quotas" ) - # def post(self, request, *args, **kwargs): - # quota = self.get_object() - # quota.save() - # return redirect( - # reverse( - # "reference_documents:version_details", - # args=[quota.reference_document_version.pk], - # ) - # + "#tariff-quotas", - # ) - class PreferentialQuotaBulkCreateView(PermissionRequiredMixin, FormView): template_name = "reference_documents/preferential_quotas/bulk_create.jinja" diff --git a/reference_documents/views/reference_document_version_views.py b/reference_documents/views/reference_document_version_views.py index 4bb9b711f..84e9c4948 100644 --- a/reference_documents/views/reference_document_version_views.py +++ b/reference_documents/views/reference_document_version_views.py @@ -194,6 +194,7 @@ def quotas_data_orders_and_rows(self): data[ref_doc_order_number.quota_order_number] = { "data_rows": [], "quota_order_number": tap_quota_order_number, + "ref_doc_order_number": ref_doc_order_number, "quota_order_number_text": ref_doc_order_number.quota_order_number, "failure_count": failure_count, "check_count": check_count,