diff --git a/src/customer/forms.py b/src/customer/forms.py index 290922f..aed7f27 100644 --- a/src/customer/forms.py +++ b/src/customer/forms.py @@ -236,14 +236,26 @@ class ClientForm(forms.ModelForm): } def __init__(self, *args, **kwargs): + self.request = kwargs.pop('request', None) super(ClientForm, self).__init__(*args, **kwargs) self.fields['inn'].widget.attrs.update(_numeric(self.fields['inn'])) self.fields['kpp'].widget.attrs.update(_numeric(self.fields['kpp'])) self.fields['ogrn'].widget.attrs.update(_numeric(self.fields['ogrn'])) self.fields['okpo'].widget.attrs.update(_numeric(self.fields['okpo'])) - # self.fields['bank_bik'].widget.attrs.update(_numeric(self.fields['bank_bik'])) - # self.fields['bank_korr_account'].widget.attrs.update(_numeric(self.fields['bank_korr_account'])) - # self.fields['bank_account'].widget.attrs.update(_numeric(self.fields['bank_account'])) + + def clean(self): + inn = self.cleaned_data.get('inn') + partner_exists = models.Client.objects.filter(inn=inn, company=self.request.user.profile) + if partner_exists: + if 'inn' not in self._errors: + from django.forms.util import ErrorList + from django.forms.forms import NON_FIELD_ERRORS + error = 'Контрагент с ИНН {} уже существует'.format(inn) + self._errors['inn'] = ErrorList() + self._errors['inn'].append(error) + self._errors[NON_FIELD_ERRORS] = self.error_class([error]) + + return self.cleaned_data class ClientAdminForm(ClientForm): @@ -267,7 +279,7 @@ class ClientsListForm(forms.Form): class UserProfileFiltersForm(MyBaseModelForm): """Общая форма фильтрации реквизитов.""" - _profile_type = None # задать в наследнике! + _profile_type = None # задать в наследнике! _is_admin = False _user = None diff --git a/src/customer/helpers.py b/src/customer/helpers.py deleted file mode 100644 index ebcc3fa..0000000 --- a/src/customer/helpers.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- -from customer.models import Client - - -def partner_already_exists(user_as_company, partner_inn): - """ - - :param user_as_company: - :param partner_inn: - :return: - """ - if Client.objects.filter(company=user_as_company, inn=partner_inn): - return True - else: - return False diff --git a/src/customer/models.py b/src/customer/models.py index 5783b34..c505488 100644 --- a/src/customer/models.py +++ b/src/customer/models.py @@ -14,6 +14,7 @@ from customer import consts, managers, utils from myauth.models import DokUser from commons.utils import only_numerics from django.utils.deconstruct import deconstructible +from django.core.exceptions import ValidationError PROFILE_IMAGES_UPLOAD_DIR = 'customer/profile/' # куда сохранять загруженные изображения BOSS_SIGN_IMG_SIZE = (159, 65) diff --git a/src/customer/views/clients_ajax.py b/src/customer/views/clients_ajax.py index e02c302..98423f8 100644 --- a/src/customer/views/clients_ajax.py +++ b/src/customer/views/clients_ajax.py @@ -12,7 +12,6 @@ from django.template.loader import render_to_string from customer import models, forms from docs.models import Invoice, Faktura, AktRabot, AktSverki, Nakladn, Platejka, Dover from customer.utils import raise_if_no_profile -from customer.helpers import partner_already_exists @login_required @@ -46,36 +45,18 @@ def clients_add_ajax(request): raise_if_no_profile(request) - form_class = forms.ClientForm + form = forms.ClientForm(request.POST, request=request) new_client_str = None - form = form_class(data=request.POST) - new_client_id = None html = '' if form.is_valid(): - # check company has in users clients - if partner_already_exists(request.user.profile, form.cleaned_data['inn']): - - data = { - 'success': False, - 'field_errors': {'inn': []}, - 'form_errors': ['Контрагент с таким ИНН уже существует.'], - 'reload': False, - 'id': None, - 'name': None, - 'action': 'client-add', - 'row_html': '', - } - - return HttpResponse(json.dumps(data), content_type='application/json') - else: - new_client = form.save(commit=False) - new_client.company = request.user.profile - new_client_str = new_client.name - new_client.save() - new_client_id = new_client.id - html = render_to_string('customer/clients/list_item.html', - {'obj': new_client}, RequestContext(request)) + new_client = form.save(commit=False) + new_client.company = request.user.profile + new_client_str = new_client.name + new_client.save() + new_client_id = new_client.id + html = render_to_string('customer/clients/list_item.html', + {'obj': new_client}, RequestContext(request)) non_field_errors = form.non_field_errors() if not form.is_valid(): @@ -83,8 +64,8 @@ def clients_add_ajax(request): data = { 'success': form.is_valid(), - 'field_errors': form.errors, # ошибки полей - 'form_errors': non_field_errors, # ошибки формы + 'field_errors': form.errors, + 'form_errors': non_field_errors, 'reload': False, 'id': new_client_id, 'name': new_client_str, diff --git a/src/dokumentor/static/js/client/search-external-api.js b/src/dokumentor/static/js/client/search-external-api.js index 3a430bf..78a19f4 100644 --- a/src/dokumentor/static/js/client/search-external-api.js +++ b/src/dokumentor/static/js/client/search-external-api.js @@ -136,14 +136,23 @@ $(document).ready(function() { }); } + function cleanFormErrors() { + var formErrors = $('.errors-layout'), + inputErrors = $('.ui-state-error'); + if (formErrors) { formErrors.hide()} + if (inputErrors) {inputErrors.removeClass('ui-state-error')} + } + + // TODO: edd clear button bankSearch.on('keyup',(function () { - // TODO: on clear button + if (bankSearch.val().length === 0) { clearResultSearchDiv($("#searchBankResult")); clearSearchInputs([bankFullName, bankBic, bankCorrespondentAccount]); } })); - // TODO: on clear button + + // TODO: edd clear button // clientSearch.on('keyup',(function () { // // if (clientSearch.val().length === 0) { @@ -151,6 +160,11 @@ $(document).ready(function() { // } // })); + clientSearch.on('keyup',(function () { + cleanFormErrors(); + })); + + fillInput(clientSearch, "PARTY", 5); fillInput(bankSearch, "BANK", 1);