167 lines
5.7 KiB
Python
167 lines
5.7 KiB
Python
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib.auth.models import User
|
|
from .models import bitreporte
|
|
from .forms import FormBitReporte, FormBitReporteEnv
|
|
from inventarioalmacen.models import area
|
|
from datetime import datetime
|
|
import datetime
|
|
from servicios.models import serviciorealizado, accesorios
|
|
from openpyxl import Workbook
|
|
from django.http.response import HttpResponse
|
|
|
|
|
|
date = datetime.datetime.now()
|
|
year = date.strftime('%y')
|
|
h = date.strftime("%H:%M:%S")
|
|
|
|
# Create your views here.
|
|
@login_required(login_url='/acceder')
|
|
def bitacora(request):
|
|
datos = bitreporte.objects.filter(pendiente='Bitacora')
|
|
servicios = serviciorealizado.objects.all()
|
|
usuarios = User.objects.all()
|
|
accesorio = accesorios.objects.all()
|
|
return render(request, "bitacora.html", {"datos": datos, "servicios": servicios, "usuarios": usuarios, "accesorio": accesorio})
|
|
|
|
|
|
@login_required(login_url='/acceder')
|
|
def crear_bitacora(request):
|
|
if request.method == "POST":
|
|
form = FormBitReporte(request.POST)
|
|
if form.is_valid():
|
|
post = form.save(commit=False)
|
|
post.save()
|
|
titulo = form.cleaned_data.get("n_reporte")
|
|
messages.success(request, f"El reporte {titulo} se ha creado correctamente")
|
|
return redirect("bitacora")
|
|
else:
|
|
for field, items in form.errors.items():
|
|
for item in items:
|
|
messages.error(request, '{}: {}'.format(field, item))
|
|
reporte = bitreporte.objects.latest('id')
|
|
date = datetime.datetime.now()
|
|
year = date.strftime('%y')
|
|
h = date.strftime("%H:%M:%S")
|
|
dato_rep = reporte.n_reporte + 1
|
|
dato_year = reporte.n_year
|
|
if year != dato_year :
|
|
new_year = year
|
|
dato_rep = 1
|
|
hora = h
|
|
form = FormBitReporteEnv()
|
|
return render(request, "crear_bitacora.html", {"form": form, "dato_rep":dato_rep, "new_year":new_year, "hora":hora})
|
|
new_year = dato_year
|
|
hora = h
|
|
form = FormBitReporteEnv()
|
|
return render(request, "crear_bitacora.html", {"form": form, "dato_rep":dato_rep, "new_year":new_year, "hora":hora})
|
|
|
|
|
|
@login_required(login_url='/acceder')
|
|
def ActualizarBitacora(request, id):
|
|
instance= get_object_or_404(bitreporte, pk=id)
|
|
form = FormBitReporte(request.POST or None, instance=instance)
|
|
context= {'form': form}
|
|
if form.is_valid():
|
|
obj= form.save(commit= False)
|
|
obj.save()
|
|
messages.success(request, "La Bitacora fue actualizada")
|
|
return redirect("bitacora")
|
|
|
|
else:
|
|
context= {'form': form, 'error': 'Error al actualizar'}
|
|
return render(request,'actualizar_bitacora.html' , context)
|
|
|
|
|
|
|
|
@login_required(login_url='/acceder')
|
|
def ReporteExcelBitacora(request):
|
|
bitacoras = bitreporte.objects.all()
|
|
ibs = User.objects.all()
|
|
bitareas = area.objects.all()
|
|
datosservicio = serviciorealizado.objects.all()
|
|
datosaccesorios = accesorios.objects.all()
|
|
fecha = datetime.datetime.now()
|
|
fe = str(fecha)
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
ws['B1'] = 'REPORTE DE SERVICIOS CONCLUIDOS'
|
|
|
|
ws.merge_cells('B1:L1')
|
|
|
|
ws['B2'] = datetime.datetime.now()
|
|
|
|
ws.merge_cells('B2:D2')
|
|
|
|
ws['B3'] = 'No. REPORTE'
|
|
ws['C3'] = 'FECHA'
|
|
ws['D3'] = 'AREA'
|
|
ws['E3'] = 'REPORTE'
|
|
ws['F3'] = 'QUIEN REPORTA'
|
|
ws['G3'] = 'ASIGNADO'
|
|
ws['H3'] = 'RECIBIO'
|
|
ws['I3'] = 'HORA'
|
|
ws['J3'] = 'SERVICIOS'
|
|
ws['K3'] = 'ACCESORIOS'
|
|
|
|
|
|
cont = 4
|
|
|
|
for bitacora in bitacoras:
|
|
for ib in ibs:
|
|
if bitacora.recibido_por_id == ib.id:
|
|
recibido = ib.username
|
|
for ib in ibs:
|
|
if bitacora.asignado_id == ib.id:
|
|
asignado = ib.username
|
|
|
|
for bitarea in bitareas:
|
|
if bitacora.area_id == bitarea.id:
|
|
datoarea = bitarea.nombre
|
|
|
|
count = 0
|
|
serreali = ""
|
|
for datoservicio in datosservicio:
|
|
if bitacora.control_bit == datoservicio.control_bit:
|
|
count = count + 1
|
|
realizado = str(count) + " - " + datoservicio.descripcion
|
|
serreali = serreali + " // " + realizado
|
|
if count == 0 :
|
|
serreali = ""
|
|
count = 0
|
|
|
|
count1 = 0
|
|
seracce = ""
|
|
for datoaccesorios in datosaccesorios:
|
|
if bitacora.control_bit == datoaccesorios.control_bit:
|
|
count1 = count1 + 1
|
|
acceso = str(count1) + " - " + datoaccesorios.n_parte + " | " + datoaccesorios.descripcion
|
|
seracce = seracce + " // " + acceso
|
|
if count1 == 0 :
|
|
seracce = ""
|
|
|
|
ws.cell(row=cont, column=2).value = bitacora.control_bit
|
|
ws.cell(row=cont, column=3).value = bitacora.fecha
|
|
ws.cell(row=cont, column=4).value = datoarea
|
|
ws.cell(row=cont, column=5).value = bitacora.reporte
|
|
ws.cell(row=cont, column=6).value = bitacora.quien_reporta
|
|
ws.cell(row=cont, column=7).value = asignado
|
|
ws.cell(row=cont, column=8).value = recibido
|
|
ws.cell(row=cont, column=9).value = bitacora.hora
|
|
ws.cell(row=cont, column=10).value = serreali
|
|
ws.cell(row=cont, column=11).value = seracce
|
|
|
|
cont += 1
|
|
serreali = ""
|
|
seracce = ""
|
|
|
|
|
|
nombre_archivo = "ReporteServicioConcluidos "+fe+".xlsx"
|
|
response = HttpResponse(content_type="application/ms-excel")
|
|
content = "attachment; filename = {0}".format(nombre_archivo)
|
|
response['Content-Disposition'] = content
|
|
wb.save(response)
|
|
return response
|
|
|