|
|
|
@ -3,11 +3,14 @@ import simplejson as json |
|
|
|
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404 |
|
|
|
from django.shortcuts import get_object_or_404 |
|
|
|
from django.http import HttpResponseBadRequest, HttpResponse |
|
|
|
from django.http import HttpResponseBadRequest, HttpResponse |
|
|
|
|
|
|
|
from django.template import RequestContext |
|
|
|
from django.views.decorators.http import require_POST |
|
|
|
from django.views.decorators.http import require_POST |
|
|
|
from django.views.decorators.csrf import csrf_protect |
|
|
|
from django.views.decorators.csrf import csrf_protect |
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
|
|
|
|
from django.template.loader import render_to_string |
|
|
|
|
|
|
|
|
|
|
|
from .. import models, forms |
|
|
|
from .. import models, forms |
|
|
|
|
|
|
|
from ...docs.models import Invoice, Faktura, AktRabot, AktSverki, Nakladn, Platejka, Dover |
|
|
|
from ..decorators import license_required |
|
|
|
from ..decorators import license_required |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -43,12 +46,16 @@ def clients_add_ajax(request): |
|
|
|
new_client_id = None |
|
|
|
new_client_id = None |
|
|
|
new_client_str = None |
|
|
|
new_client_str = None |
|
|
|
form = form_class(data=request.POST) |
|
|
|
form = form_class(data=request.POST) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new_client_id = None |
|
|
|
|
|
|
|
html = '' |
|
|
|
if form.is_valid(): |
|
|
|
if form.is_valid(): |
|
|
|
new_client = form.save(commit=False) |
|
|
|
new_client = form.save(commit=False) |
|
|
|
new_client.user = request.user |
|
|
|
new_client.user = request.user |
|
|
|
new_client.save() |
|
|
|
new_client.save() |
|
|
|
new_client_id = new_client.id |
|
|
|
new_client_id = new_client.id |
|
|
|
new_client_str = new_client.name |
|
|
|
html = render_to_string('customer/clients/list_item.html', |
|
|
|
|
|
|
|
{'obj': new_client}, RequestContext(request)) |
|
|
|
|
|
|
|
|
|
|
|
non_field_errors = form.non_field_errors() |
|
|
|
non_field_errors = form.non_field_errors() |
|
|
|
if not form.is_valid(): |
|
|
|
if not form.is_valid(): |
|
|
|
@ -59,9 +66,10 @@ def clients_add_ajax(request): |
|
|
|
'field_errors': form.errors, # ошибки полей |
|
|
|
'field_errors': form.errors, # ошибки полей |
|
|
|
'form_errors': non_field_errors, # ошибки формы |
|
|
|
'form_errors': non_field_errors, # ошибки формы |
|
|
|
#'reload': form.is_valid() and 'reload_on_success' in request.GET |
|
|
|
#'reload': form.is_valid() and 'reload_on_success' in request.GET |
|
|
|
'reload': True, |
|
|
|
'reload': False, |
|
|
|
'id': new_client_id, |
|
|
|
'id': new_client_id, |
|
|
|
'name': new_client_str, |
|
|
|
'action': 'client-add', |
|
|
|
|
|
|
|
'row_html': html, |
|
|
|
} |
|
|
|
} |
|
|
|
return HttpResponse(json.dumps(data), mimetype='application/json') |
|
|
|
return HttpResponse(json.dumps(data), mimetype='application/json') |
|
|
|
|
|
|
|
|
|
|
|
@ -80,9 +88,11 @@ def clients_edit_ajax(request, id): |
|
|
|
|
|
|
|
|
|
|
|
form = form_class(data=request.POST, instance=client) |
|
|
|
form = form_class(data=request.POST, instance=client) |
|
|
|
if form.is_valid(): |
|
|
|
if form.is_valid(): |
|
|
|
form.save() |
|
|
|
client = form.save() |
|
|
|
|
|
|
|
|
|
|
|
non_field_errors = form.non_field_errors() |
|
|
|
non_field_errors = form.non_field_errors() |
|
|
|
|
|
|
|
html = render_to_string('customer/clients/list_item.html', |
|
|
|
|
|
|
|
{'obj': client}, RequestContext(request)) |
|
|
|
if not form.is_valid(): |
|
|
|
if not form.is_valid(): |
|
|
|
non_field_errors.append(u'Заполните/исправьте выделенные поля.') |
|
|
|
non_field_errors.append(u'Заполните/исправьте выделенные поля.') |
|
|
|
|
|
|
|
|
|
|
|
@ -90,7 +100,13 @@ def clients_edit_ajax(request, id): |
|
|
|
'success': form.is_valid(), |
|
|
|
'success': form.is_valid(), |
|
|
|
'field_errors': form.errors, # ошибки полей |
|
|
|
'field_errors': form.errors, # ошибки полей |
|
|
|
'form_errors': non_field_errors, # ошибки формы |
|
|
|
'form_errors': non_field_errors, # ошибки формы |
|
|
|
'reload': True, |
|
|
|
'reload': False, |
|
|
|
|
|
|
|
'id': client.id, |
|
|
|
|
|
|
|
'name': client.name, |
|
|
|
|
|
|
|
'contact': client.contact_name, |
|
|
|
|
|
|
|
'phone': client.contact_phone, |
|
|
|
|
|
|
|
'action': 'client-edit', |
|
|
|
|
|
|
|
'row_html': html, |
|
|
|
} |
|
|
|
} |
|
|
|
return HttpResponse(json.dumps(data), mimetype='application/json') |
|
|
|
return HttpResponse(json.dumps(data), mimetype='application/json') |
|
|
|
|
|
|
|
|
|
|
|
@ -104,12 +120,30 @@ def clients_delete_ajax(request, id): |
|
|
|
return HttpResponseBadRequest() |
|
|
|
return HttpResponseBadRequest() |
|
|
|
|
|
|
|
|
|
|
|
client = get_object_or_404(models.Client, pk=id, user=request.user) |
|
|
|
client = get_object_or_404(models.Client, pk=id, user=request.user) |
|
|
|
client.delete() |
|
|
|
client_docs = [] |
|
|
|
|
|
|
|
doc_list = [(Invoice, u'счета'), (Faktura, u'счета-фактуры'), (Nakladn, u'накладные'), |
|
|
|
|
|
|
|
(AktRabot, u'акты выполненных работ'), (Platejka, u'платёжные поручения'), |
|
|
|
|
|
|
|
(Dover, u'доверенности'), (AktSverki, u'акты сверки')] |
|
|
|
|
|
|
|
for doc in doc_list: |
|
|
|
|
|
|
|
docs = doc[0].objects.filter(client=client) |
|
|
|
|
|
|
|
if docs: |
|
|
|
|
|
|
|
client_docs.append(doc[1]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not client_docs: |
|
|
|
|
|
|
|
client.delete() |
|
|
|
|
|
|
|
success = True |
|
|
|
|
|
|
|
message = {'title': u'Инфо', 'msg': u'Контрагент удалён.'} |
|
|
|
|
|
|
|
del_id = id |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
success = True |
|
|
|
|
|
|
|
message = {'title': u'Инфо', 'msg': u'Контрагент не удалён. Есть выписанные документы: %s.' % ','.join(client_docs)} |
|
|
|
|
|
|
|
del_id = None |
|
|
|
|
|
|
|
|
|
|
|
# TODO обработать ошибки удаления |
|
|
|
|
|
|
|
data = { |
|
|
|
data = { |
|
|
|
'success': True, |
|
|
|
'success': success, |
|
|
|
'message': {'title': 'Инфо', 'msg': 'Контрагент удалён.',}, |
|
|
|
'message': message, |
|
|
|
'reload': True, |
|
|
|
'reload': False, |
|
|
|
|
|
|
|
'action': 'client-delete', |
|
|
|
|
|
|
|
'id': del_id, |
|
|
|
} |
|
|
|
} |
|
|
|
return HttpResponse(json.dumps(data), mimetype='application/json') |
|
|
|
return HttpResponse(json.dumps(data), mimetype='application/json') |
|
|
|
|