diff --git a/project/customer/views/clients_ajax.py b/project/customer/views/clients_ajax.py index 7fbcf73..124afeb 100644 --- a/project/customer/views/clients_ajax.py +++ b/project/customer/views/clients_ajax.py @@ -3,11 +3,14 @@ import simplejson as json from django.shortcuts import get_object_or_404 from django.http import HttpResponseBadRequest, HttpResponse +from django.template import RequestContext from django.views.decorators.http import require_POST from django.views.decorators.csrf import csrf_protect from django.contrib.auth.decorators import login_required +from django.template.loader import render_to_string from .. import models, forms +from ...docs.models import Invoice, Faktura, AktRabot, AktSverki, Nakladn, Platejka, Dover from ..decorators import license_required @@ -43,12 +46,16 @@ def clients_add_ajax(request): new_client_id = None new_client_str = None form = form_class(data=request.POST) + + new_client_id = None + html = '' if form.is_valid(): new_client = form.save(commit=False) new_client.user = request.user new_client.save() 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() if not form.is_valid(): @@ -59,9 +66,10 @@ def clients_add_ajax(request): 'field_errors': form.errors, # ошибки полей 'form_errors': non_field_errors, # ошибки формы #'reload': form.is_valid() and 'reload_on_success' in request.GET - 'reload': True, + 'reload': False, 'id': new_client_id, - 'name': new_client_str, + 'action': 'client-add', + 'row_html': html, } 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) if form.is_valid(): - form.save() + client = form.save() 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(): non_field_errors.append(u'Заполните/исправьте выделенные поля.') @@ -90,7 +100,13 @@ def clients_edit_ajax(request, id): 'success': form.is_valid(), 'field_errors': form.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') @@ -104,12 +120,30 @@ def clients_delete_ajax(request, id): return HttpResponseBadRequest() 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 = { - 'success': True, - 'message': {'title': 'Инфо', 'msg': 'Контрагент удалён.',}, - 'reload': True, + 'success': success, + 'message': message, + 'reload': False, + 'action': 'client-delete', + 'id': del_id, } return HttpResponse(json.dumps(data), mimetype='application/json') diff --git a/project/static/js/client.commons.js b/project/static/js/client.commons.js index f9e5f02..6956b7d 100644 --- a/project/static/js/client.commons.js +++ b/project/static/js/client.commons.js @@ -1,14 +1,25 @@ function setup_client_edit_form(form) { + var btn1_caption = form.data('dialog-btn-caption') || "Сохранить"; form.dialog({ modal: true, autoOpen: false, - minWidth: 670 + minWidth: 670, + buttons: [ + { + text: btn1_caption, + click: function(){ + form.submit() + }, + }, + { + text:"Отмена", + click: function(){ + $(this).dialog("close"); + }, + } + ] }); - $('button[name=close-form]', form).click(function() { - form.dialog('close'); - return false; - }); } function setup_client_delete_form(form) { @@ -17,15 +28,11 @@ function setup_client_delete_form(form) { autoOpen: false }); - $('button[name=close-form]', form).click(function() { - form.dialog('close'); - return false; - }); } function setup_client_edit_links(form, reload_on_success) { $('table#clients td a.client.edit-link').each(function() { - $(this).click(function() { + $(this).on('click', function() { var link = $(this); var form_action = link.attr('href') + 'ajax/'; // url to post form diff --git a/project/static/js/client.list.js b/project/static/js/client.list.js index fd963e2..e7bb316 100644 --- a/project/static/js/client.list.js +++ b/project/static/js/client.list.js @@ -1,14 +1,17 @@ $(document).ready(function() { - var edit_form = $('#client-edit-form'); - if (edit_form) { - setup_client_edit_form(edit_form); - setup_client_edit_links(edit_form, true); - setup_client_add_link(edit_form, true); - } + window.clients_edit_del_update = function() { + var edit_form = $('#client-edit-form'); + if (edit_form) { + setup_client_edit_form(edit_form); + setup_client_edit_links(edit_form, true); + setup_client_add_link(edit_form, true); + } - var delete_form = $('#client-delete-form'); - if (delete_form) { - setup_client_edit_form(delete_form); - setup_client_delete_links(delete_form, true); + var delete_form = $('#client-delete-form'); + if (delete_form) { + setup_client_edit_form(delete_form); + setup_client_delete_links(delete_form, true); + } } + window.clients_edit_del_update(); }); diff --git a/project/static/js/dialogs.js b/project/static/js/dialogs.js index 33cdd7b..8f9c2ff 100644 --- a/project/static/js/dialogs.js +++ b/project/static/js/dialogs.js @@ -41,6 +41,19 @@ $(document).ready(function() { dlg_msg.dialog('open'); } } + if (data.action){ + if (data.action == 'client-add'){ + $(data.row_html).insertAfter('#clients_header_row'); + window.clients_edit_del_update(); + } + if (data.action == 'client-edit'){ + $("tr#client_" + data.id).replaceWith(data.row_html); + window.clients_edit_del_update(); + } + if (data.action == 'client-delete'){ + $("tr#client_" + data.id).remove(); + } + } else if (data.reload) { window.location.reload(true); } diff --git a/project/templates/customer/clients/form.html b/project/templates/customer/clients/form.html index 89bb83e..072e0ab 100644 --- a/project/templates/customer/clients/form.html +++ b/project/templates/customer/clients/form.html @@ -206,9 +206,6 @@ {% if not is_ajax %} - {% else %} - - {% endif %} diff --git a/project/templates/customer/clients/list.html b/project/templates/customer/clients/list.html index fed1e1b..532b9a1 100644 --- a/project/templates/customer/clients/list.html +++ b/project/templates/customer/clients/list.html @@ -15,7 +15,7 @@

- + @@ -35,12 +35,10 @@ {% block dialogs %} {% include "customer/clients/form.html" with form=client_form is_ajax="True" %} - + {% csrf_token %}

Вы уверены, что хотите удалить контрагента ?

- - {% endblock %}
Контрагент Контактное лицо Телефон