You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.5 KiB
83 lines
2.5 KiB
# -*- coding: utf-8 -*-
|
|
import simplejson as json
|
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
from django.http import HttpResponseBadRequest, HttpResponse
|
|
from django.core import serializers
|
|
from django.db.models import get_model
|
|
from django.views.decorators.csrf import csrf_protect
|
|
|
|
from ..models import Invoice
|
|
from project.customer.utils import raise_if_no_profile
|
|
|
|
|
|
def get_pair(request, model, param1, val1, param2):
|
|
if not request.is_ajax():
|
|
return HttpResponseBadRequest()
|
|
|
|
kwargs = {param1: val1}
|
|
|
|
try:
|
|
q_model = get_model('docs', model)
|
|
val2 = getattr(q_model.objects.get(**kwargs), param2)
|
|
except:
|
|
val2 = None
|
|
data = {
|
|
'val': val2,
|
|
}
|
|
return HttpResponse(json.dumps(data), mimetype='application/json')
|
|
|
|
|
|
def get_invoices(request, client_id=None):
|
|
if not request.is_ajax():
|
|
return HttpResponseBadRequest()
|
|
|
|
raise_if_no_profile(request)
|
|
|
|
if client_id:
|
|
invoices = Invoice.objects.filter(client__id=client_id)
|
|
else:
|
|
invoices = Invoice.objects.filter(company=request.user.profile)
|
|
invoices = {invoice.id: '№ %s от %s' % (invoice.doc_num, invoice.doc_date) for invoice in invoices}
|
|
|
|
return HttpResponse(json.dumps(invoices), mimetype='application/json')
|
|
|
|
|
|
def get_tbl_items(request, invoice_id):
|
|
if not request.is_ajax():
|
|
return HttpResponseBadRequest()
|
|
|
|
invoice = Invoice.objects.get(pk=invoice_id)
|
|
data = serializers.serialize('json', invoice.invoice_items.all())
|
|
|
|
return HttpResponse(json.dumps(data), mimetype='application/json')
|
|
|
|
|
|
def get_client_by_invoice(request, invoice_id):
|
|
if not request.is_ajax():
|
|
return HttpResponseBadRequest()
|
|
|
|
invoice = Invoice.objects.get(pk=invoice_id)
|
|
|
|
return HttpResponse(json.dumps([invoice.client.id, invoice.client.name]), mimetype='application/json')
|
|
|
|
|
|
def toggle_doc_status(request, doc_type, doc_id, doc_attr):
|
|
if not request.is_ajax() or request.method != 'POST':
|
|
return HttpResponseBadRequest()
|
|
|
|
model_ = get_model('docs', doc_type)
|
|
doc = model_.objects.get(pk=doc_id)
|
|
choices_ = doc._meta.get_field_by_name(doc_attr)[0].get_choices()
|
|
choices = [i[0] for i in choices_[1:]]
|
|
prev_val = getattr(doc, doc_attr)
|
|
try:
|
|
next_index = choices.index(prev_val) + 1
|
|
next_val = choices[next_index]
|
|
except:
|
|
next_val = choices[0]
|
|
setattr(doc, doc_attr, next_val)
|
|
doc.save()
|
|
next_text = dict(choices_)[next_val]
|
|
|
|
return HttpResponse(json.dumps([next_text, next_val]), mimetype='application/json')
|
|
|