277 lines
9.9 KiB
Python
277 lines
9.9 KiB
Python
from django.shortcuts import render, redirect
|
|
from django.urls import reverse_lazy
|
|
from django.views import View
|
|
from datetime import datetime
|
|
from django.contrib import messages
|
|
from django.contrib.auth.views import LogoutView, LoginView
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib.auth import logout
|
|
from django.conf import settings
|
|
from django.http import HttpRequest, HttpResponse, QueryDict, HttpResponseRedirect
|
|
from django.template.loader import get_template
|
|
from xhtml2pdf import pisa
|
|
from django.contrib.staticfiles import finders
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
|
from django.views.generic import RedirectView
|
|
from django.db.models import Sum
|
|
from django.contrib.auth.models import User
|
|
from .models import Unidad, Articulo, ArticuloUnidad, PruebaUnidad
|
|
from .forms import FormUnidad , FormArticulo, FormEnvio
|
|
|
|
# vista de inventario
|
|
|
|
class VistaInventario(LoginRequiredMixin,View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
articulo = Articulo.objects.all()
|
|
unidades = Unidad.objects.all().count()
|
|
usuarios = User.objects.all().count()
|
|
context = {'articulo':articulo, 'unidades':unidades, 'usuarios':usuarios}
|
|
return render(request, 'articulos/index.html', context)
|
|
|
|
|
|
class CrearArticulo(LoginRequiredMixin, CreateView):
|
|
form_class = FormArticulo
|
|
initial = {"key": "value"}
|
|
template_name = "articulos/crear.html"
|
|
def get(self, request, *args, **kwargs):
|
|
formulario_articulo = self.form_class(initial=self.initial)
|
|
context = {'formulario_articulo':formulario_articulo}
|
|
return render(request, self.template_name, context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
formulario_articulo =self.form_class(request.POST)
|
|
if formulario_articulo.is_valid():
|
|
formulario_articulo.save()
|
|
messages.success(request,'Se registro Carpas con Exito')
|
|
return redirect('articulos')
|
|
return render(request, self.template_name, {'formulario_articulo':formulario_articulo})
|
|
|
|
|
|
class EditarVista(LoginRequiredMixin, UpdateView):
|
|
model = Articulo
|
|
form_class = FormArticulo
|
|
template_name = "articulos/editar.html"
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
return super().get(request, self.template_name, *args, **kwargs)
|
|
|
|
def get_initial(self):
|
|
initial = super().get_initial()
|
|
# initial['serial'] = self.object.serial
|
|
return initial
|
|
|
|
def form_valid(self, form):
|
|
if form.is_valid():
|
|
self.object = form.save()
|
|
return redirect('articulos')
|
|
else:
|
|
return render(self.template_name, {'form': form})
|
|
|
|
class EliminarVista(LoginRequiredMixin, DeleteView):
|
|
model = Articulo
|
|
template_name = "articulos/eliminar.html"
|
|
success_url = reverse_lazy('articulos')
|
|
|
|
|
|
class ComprobanteVista(LoginRequiredMixin, View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
try:
|
|
articulos = ArticuloUnidad.objects.all()
|
|
fecha = datetime.now().date()
|
|
img_uno = settings.STATIC_ROOT + '/imagenes/imagen.png'
|
|
img_dos = settings.STATIC_ROOT + '/imagenes/logo.png'
|
|
template_path = "articulos/comprobante.html"
|
|
context = {
|
|
"articulos": articulos,
|
|
"fecha": fecha,
|
|
"img_uno": img_uno,
|
|
"img_dos": img_dos,
|
|
}
|
|
response = HttpResponse(content_type="application/pdf")
|
|
response["Content-Disposition"] = (
|
|
'attachment; filename="comprobantes.pdf"'
|
|
)
|
|
template = get_template(template_path)
|
|
html = template.render(context)
|
|
pisa_status = pisa.CreatePDF(html, dest=response)
|
|
if pisa_status.err:
|
|
return HttpResponse("We had some errors <pre>" + html + "</pre>")
|
|
return response
|
|
except:
|
|
return render(request, "articulos/comprobante.html")
|
|
|
|
# ---------- fin de la vista inventario
|
|
|
|
# inicio de la vista de envio de articulos
|
|
|
|
|
|
class VistaEnvio(LoginRequiredMixin, CreateView):
|
|
form_class = FormEnvio
|
|
initial = {"key": "value"}
|
|
template_name = "envios/envio_articulo.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["formulario_envio"] = self.get_form()
|
|
return context
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
articulo = Articulo.objects.all()
|
|
articulos_c = ArticuloUnidad.objects.all()
|
|
unidad = Unidad.objects.all()
|
|
context = {"articulo": articulo, "unidad": unidad, "articulos_c": articulos_c}
|
|
return render(request, self.template_name, context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
formulario_envio = self.form_class(request.POST)
|
|
|
|
# Verifica si el formulario es válido
|
|
if formulario_envio.is_valid():
|
|
unidad = formulario_envio.cleaned_data["unidad"]
|
|
cantidad = formulario_envio.cleaned_data["cantidad"]
|
|
articulo = formulario_envio.cleaned_data["articulo"]
|
|
|
|
if articulo.cantidad < cantidad:
|
|
messages.error(request, "No hay suficientes artículos en stock.")
|
|
return redirect("envio_articulo")
|
|
|
|
# Proceder con el envío si hay suficientes artículos
|
|
articulo.cantidad -= cantidad
|
|
articulo.save()
|
|
|
|
ArticuloUnidad.objects.create(
|
|
movimiento="ENVIADO",
|
|
articulo=articulo,
|
|
unidad=unidad,
|
|
cantidad=cantidad,
|
|
descripcion=articulo.descripcion,
|
|
)
|
|
|
|
PruebaUnidad.objects.create(
|
|
movimiento="RECIBIDO",
|
|
articulo=articulo,
|
|
unidad=unidad,
|
|
cantidad=cantidad,
|
|
descripcion=articulo.descripcion,
|
|
)
|
|
messages.success(request,"Se registro con exito.")
|
|
return redirect("envio_articulo")
|
|
else:
|
|
messages.error(request,"rellene los campos")
|
|
return redirect('envio_articulo')
|
|
# fin de la vista de envio de articulos
|
|
|
|
|
|
class BorrarVistaComprobante(LoginRequiredMixin, View):
|
|
template_name = "envios/borrartodo.html"
|
|
success_url = reverse_lazy("envio_articulo")
|
|
def get(self, request):
|
|
return render(request, self.template_name)
|
|
def post(self, request):
|
|
ArticuloUnidad.objects.all().delete()
|
|
messages.success(request,'se elimino todo')
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
|
|
# fin de la vista de articulos
|
|
|
|
|
|
class VistaUnidad(LoginRequiredMixin, View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
unidad = Unidad.objects.all()
|
|
context = {'unidad':unidad}
|
|
return render(request, 'unidad/unidad_index.html',context)
|
|
|
|
|
|
class CrearUnidad(LoginRequiredMixin, CreateView):
|
|
form_class = FormUnidad
|
|
initial = {"key": "value"}
|
|
template_name = "unidad/crear_unidad.html"
|
|
def get(self, request, *args, **kwargs):
|
|
formulario_unidad = self.form_class(initial=self.initial)
|
|
context = {'formulario_unidad':formulario_unidad}
|
|
return render(request, self.template_name, context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
formulario_unidad = self.form_class(request.POST)
|
|
if formulario_unidad.is_valid():
|
|
formulario_unidad.save()
|
|
return redirect('unidad')
|
|
return render(request, self.template_name, {'formulario_unidad':formulario_unidad})
|
|
|
|
|
|
class UnidadArticulo(LoginRequiredMixin, View):
|
|
def get(self, request, id):
|
|
unidad= Unidad.objects.get(pk=id)
|
|
articulo = PruebaUnidad.objects.filter(unidad=unidad)
|
|
context={'articulo':articulo, 'unidad':unidad}
|
|
return render(request, 'subunidad/unidad_articulo.html',context)
|
|
|
|
|
|
class EditarVistaUnidad(LoginRequiredMixin, UpdateView):
|
|
model = Unidad
|
|
form_class = FormUnidad
|
|
template_name = "unidad/editar_unidad.html"
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
return super().get(request, self.template_name, *args, **kwargs)
|
|
|
|
def get_initial(self):
|
|
initial = super().get_initial()
|
|
return initial
|
|
|
|
def form_valid(self, form):
|
|
self.object = form.save()
|
|
return redirect('unidad')
|
|
|
|
|
|
class EliminarVistaUnidad(LoginRequiredMixin, DeleteView):
|
|
model = Unidad
|
|
template_name = "unidad/eliminar_unidad.html"
|
|
success_url = reverse_lazy('unidad')
|
|
|
|
|
|
class ListaVista(LoginRequiredMixin, View):
|
|
|
|
def get(self, request, id, *args, **kwargs):
|
|
try:
|
|
unidad = Unidad.objects.get(pk=id)
|
|
articulos = PruebaUnidad.objects.filter(unidad=id)
|
|
fecha = datetime.now().date()
|
|
img_uno = settings.STATIC_ROOT + "/imagenes/imagen.png"
|
|
img_dos = settings.STATIC_ROOT + "/imagenes/logo.png"
|
|
template_path = "subunidad/lista.html"
|
|
context = {
|
|
"articulos": articulos,
|
|
"fecha": fecha,
|
|
"img_uno": img_uno,
|
|
"img_dos": img_dos,
|
|
}
|
|
response = HttpResponse(content_type="application/pdf")
|
|
response["Content-Disposition"] = 'attachment; filename="comprobantes.pdf"'
|
|
template = get_template(template_path)
|
|
html = template.render(context)
|
|
pisa_status = pisa.CreatePDF(html, dest=response)
|
|
if pisa_status.err:
|
|
return HttpResponse("We had some errors <pre>" + html + "</pre>")
|
|
return response
|
|
except:
|
|
return render(request, "subunidad/lista.html")
|
|
|
|
|
|
class AccesoVista(LoginView):
|
|
login_url = "registration/login.html"
|
|
redirect_field_name = "redirect_to"
|
|
|
|
|
|
class CerrarVista(RedirectView):
|
|
def get(self, request):
|
|
logout(request)
|
|
return redirect("login")
|