#encoding:utf-8 from django.core.urlresolvers import reverse from django.shortcuts import * #Libreria para el HttpResponse from django.contrib.auth.decorators import * # Libreria para el login_required from django.contrib import messages from django.http import * from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework import status from apps.control.forms import * from apps.programacion_de_entrega.forms import * from django.db import transaction #LIBRERIAS PARA PDF CON'PISA# from django import http from django.template.loader import get_template from django.template import Context import ho.pisa as pisa import cStringIO as StringIO from django.utils.encoding import smart_str import cgi from django.views.decorators.csrf import csrf_exempt @login_required(login_url='/login') def programacion_entrega( request) : municipios = Municipio.objects.all() vehiculos = CatVehiculo.objects.all().order_by('marca') contexto = {'reg_programacion':'active','municipios':municipios,'vehiculos':vehiculos} return render_to_response('programacion_entrega.html', contexto ,context_instance = RequestContext(request)) @login_required(login_url='/login') def vehiculo_entrega(request, municipio, mes, ano, vehiculo,ruta_combo): validar=None mes_abasto = None validar_programacion=None tonelaje = 0 listaresguardos= ProgramacionEntregaEscuela.objects.filter(estatus='RESGUARDO').distinct('escuela') listaresguardos2= ProgramacionEntregaEscuela.objects.filter(estatus='RESGUARDO') # vehiculo = CatVehiculo.objects.get( pk = vehiculo ) # capacidad_vehiculo = (float(vehiculo.capacidad) * 1000) lista_vehiculos = CatVehiculo.objects.all().order_by('capacidad','numPlaca') choferes = CatPersona.objects.filter( status = True, tipoPersona__clave = 'CHOF') try: validar_programacion = ValidarProgramacion.objects.get( Municipio = municipio, mes = mes, ano = ano) except ValidarProgramacion.DoesNotExist: pass try: validar = ValidarCuadroAbasto.objects.get( Municipio = municipio, mes = mes, ano = ano) except ValidarCuadroAbasto.DoesNotExist: pass try: leche_t = CatPaquete.objects.get( status = True, tipoPaquete__clave = 'L') paquete_t = CatPaquete.objects.get( status = True, tipoPaquete__clave = 'PA') except CatPaquete.DoesNotExist: pass municipios = Municipio.objects.get( pk = municipio ) municipios_rutas_combo = CatRuta.objects.filter( Localidad__municipio = municipio ).distinct() municipios_rutas=municipios_rutas_combo.filter(pk=ruta_combo) diccionariopeso_total_t = {} diccionatio_tot_ent_leche = {} for ruta in municipios_rutas: totalescuelas=[] t_leche = 0 totalpaquete = 0 peso_total_t = 0 for loc in ruta.Localidad.all(): if loc.municipio.nombre == municipios.nombre: for escuela in loc.catescuela_set.all(): if ruta.nombre == escuela.ruta.nombre: if escuela.status == True: for ninos in escuela.registroabasto_set.all(): if mes == ninos.mes and ano == ninos.ano: mes_abasto = ninos.mes if mes == "Abril" or mes == "Agosto" or mes == "Diciembre": t_leche = t_leche+int(ninos.totalNinosLeche * 2) else: t_leche = t_leche+int(ninos.totalNinosLeche * 4) if Dotacion.objects.filter(registro_abasto__pk=ninos.id).count()==0: if ninos.totalNinos >= 5 and ninos.totalNinos < 10: resultado = 1 else: resultado = ninos.totalNinos % 10 if resultado == 0: resultado = ninos.totalNinos / 10 elif resultado > 5: resultado = ((ninos.totalNinos / 10) + 1) elif resultado <= 5: resultado = ninos.totalNinos / 10 totalpaquete=totalpaquete+resultado else: dotacion = Dotacion.objects.get(registro_abasto__pk=ninos.id) resultado = dotacion.paquetes resultado2 = dotacion.leches totalpaquete=totalpaquete+resultado t_leche=t_leche+resultado2 #TONELAJE PARA REPARTIR POR ESCUELA# if mes == "Abril" or mes == "Agosto" or mes == "Diciembre": t_leche = ninos.totalNinosLeche * 2 else: t_leche = ninos.totalNinosLeche * 4 peso_paquete_leche = leche_t.peso peso_paquete_alimentario = paquete_t.peso dividir_peso_botes = (float(peso_paquete_leche) / 12) peso_total_leche = (float(t_leche) * float(dividir_peso_botes)) peso_total_paq_ali = (float(peso_paquete_alimentario) * resultado ) peso_total_t = (peso_total_leche + peso_total_paq_ali) diccionariopeso_total_t[ninos.id] = str(int(peso_total_t)) # END TONELAJE POR ESCUELA# if t_leche%12 == 0: tot_ent_leche = (str(t_leche/12)+" paquete(s)") else: tot_ent_leche = (str(t_leche/12)+" paquete(s) y "+str(t_leche%12)+" pieza(s)") if t_leche != 0: diccionatio_tot_ent_leche[escuela.id] = tot_ent_leche ############### for lista in listaresguardos: if lista.escuela.id == ninos.escuela.id: for res in lista.escuela.registroabasto_set.all(): if res.enviado == None: if res.mes == "Abril" or res.mes == "Agosto" or res.mes == "Diciembre": t_leche = t_leche+int(res.totalNinosLeche * 2) else: t_leche = t_leche+int(res.totalNinosLeche * 4) if Dotacion.objects.filter(registro_abasto__pk=res.id).count()==0: if res.totalNinos >= 5 and res.totalNinos < 10: resultado = 1 else: resultado = res.totalNinos % 10 if resultado == 0: resultado = res.totalNinos / 10 elif resultado > 5: resultado = ((res.totalNinos / 10) + 1) elif resultado <= 5: resultado = res.totalNinos / 10 totalpaquete=totalpaquete+resultado else: dotacion = Dotacion.objects.get(registro_abasto__pk=res.id) resultado = dotacion.paquetes resultado2 = dotacion.leches totalpaquete=totalpaquete+resultado t_leche=t_leche+resultado2 #TONELAJE PARA REPARTIR POR ESCUELA# if res.mes == "Abril" or res.mes == "Agosto" or res.mes == "Diciembre": t_leche = res.totalNinosLeche * 2 else: t_leche = res.totalNinosLeche * 4 peso_paquete_leche = leche_t.peso peso_paquete_alimentario = paquete_t.peso dividir_peso_botes = (float(peso_paquete_leche) / 12) peso_total_leche = (float(t_leche) * float(dividir_peso_botes)) peso_total_paq_ali = (float(peso_paquete_alimentario) * resultado ) peso_total_t = (peso_total_leche + peso_total_paq_ali) diccionariopeso_total_t[res.id] = str(int(peso_total_t)) # END TONELAJE POR ESCUELA# if t_leche%12 == 0: tot_ent_leche = (str(t_leche/12)+" paquete(s)") else: tot_ent_leche = (str(t_leche/12)+" paquete(s) y "+str(t_leche%12)+" pieza(s)") if t_leche != 0: diccionatio_tot_ent_leche[escuela.id] = tot_ent_leche else: pass # contexto= {'validar_programacion':validar_programacion,'listaresguardos2':listaresguardos2,'listaresguardos':listaresguardos,'diccionatio_tot_ent_leche':diccionatio_tot_ent_leche,'choferes':choferes,'diccionariopeso_total_t':diccionariopeso_total_t,'vehiculo':vehiculo,'capacidad_vehiculo':capacidad_vehiculo,'mes_abasto':mes_abasto,'validar':validar,'mes':mes,'ano':ano,'municipios':municipios,'municipios_rutas':municipios_rutas, 'reg_programacion':'active'} # return render_to_response('vehiculo_entrega.html', contexto ,context_instance = RequestContext(request)) contexto= {'municipios_rutas_combo':municipios_rutas_combo,'lista_vehiculos':lista_vehiculos,'validar_programacion':validar_programacion,'listaresguardos2':listaresguardos2,'listaresguardos':listaresguardos,'diccionatio_tot_ent_leche':diccionatio_tot_ent_leche,'choferes':choferes,'diccionariopeso_total_t':diccionariopeso_total_t,'vehiculo':vehiculo,'mes_abasto':mes_abasto,'validar':validar,'mes':mes,'ano':ano,'municipios':municipios,'municipios_rutas':municipios_rutas, 'reg_programacion':'active'} return render_to_response('vehiculo_entrega.html', contexto ,context_instance = RequestContext(request)) @csrf_exempt @login_required(login_url='/login') def guardar_entrega(request): if request.is_ajax(): mes = request.GET.get('mes') ano = request.GET.get('ano') id_municipio = request.GET.get('municipio') municipio = Municipio.objects.get( pk = id_municipio) escuela_id = request.GET.getlist('data[]') id_chofer = request.GET.get('chofer') chofer = CatPersona.objects.get( pk = id_chofer ) id_vehiculo = request.GET.get('vehiculo') vehiculo = CatVehiculo.objects.get( pk = id_vehiculo ) resguardolista=request.GET.getlist('resguardolista[]') resguardoabasto=request.GET.getlist('resguardoabasto[]') tabla = ProgramacionEntrega( mes = mes, ano = ano, municipio = municipio, nombre_chofer = chofer, vehiculo = vehiculo ) tabla.save() if not resguardolista: abasto_id = request.GET.getlist('abasto_id[]') for abasto in abasto_id: reg_abasto = RegistroAbasto.objects.get( pk = abasto ) reg_abasto.enviado = True reg_abasto.save() #for escuela in escuela_id: esc = CatEscuela.objects.get(pk=reg_abasto.escuela.id) Programacion = ProgramacionEntregaEscuela(programacion = tabla, escuela=esc, estatus="ACTIVO",registro_abasto=reg_abasto.id) Programacion.save() else: #ENCONTRO UN RESGUARDO #abasto_id = request.GET.getlist('resguardoabasto[]') for abasto in resguardoabasto: reg_abasto = RegistroAbasto.objects.get( pk = abasto ) reg_abasto.enviado = True reg_abasto.save() #for escuela in escuela_id: for lista in resguardolista: lis=ProgramacionEntregaEscuela.objects.get(pk=lista) if lis.registro_abasto == reg_abasto.id: esc = CatEscuela.objects.get(pk=reg_abasto.escuela.id) Programacion = ProgramacionEntregaEscuela() Programacion.id=lis.id Programacion.programacion=tabla Programacion.escuela=esc Programacion.estatus='ACTIVO' Programacion.registro_abasto=reg_abasto.id Programacion.save() abasto_id = request.GET.getlist('abasto_id[]') for abasto in abasto_id: reg_abasto = RegistroAbasto.objects.get( pk = abasto ) reg_abasto.enviado = True reg_abasto.save() esc = CatEscuela.objects.get(pk=reg_abasto.escuela.id) Programacion = ProgramacionEntregaEscuela(programacion = tabla, escuela=esc, estatus="ACTIVO",registro_abasto=reg_abasto.id) Programacion.save() data = 'Su entrega se a guardado correctamente' return HttpResponse(data) @login_required(login_url='/login') def buscar_programacion( request ): municipios = Municipio.objects.all() firmas = CatPersona.objects.all() contexto = {'reg_programacion':'active','municipios':municipios, 'firma':firmas} return render_to_response('buscar_programacion.html', contexto ,context_instance = RequestContext(request)) @login_required(login_url='/login') def vales_salida( request ): form = ValesSalidaForm() contexto = {'reg_programacion':'active','form':form} return render_to_response('vales_salida.html', contexto, context_instance = RequestContext( request )) def render_to_pdf(template_src, context_dict): template = get_template(template_src) context = Context(context_dict) html = template.render(context) result = StringIO.StringIO() pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result,encoding='UTF-8') if not pdf.err: return http.HttpResponse(result.getvalue(), mimetype='application/pdf') return http.HttpResponse('We had some errors
%s
' % cgi.escape(html)) @login_required(login_url='/login') def programacion_mes_pdf( request, mes, ano, municipios, elaboro, vo): p_entrega = ProgramacionEntrega.objects.filter( mes = mes, ano = ano, municipio_id=municipios) entrega = ProgramacionEntregaEscuela.objects.filter(estatus='ACTIVO', programacion__ano=ano, programacion__mes=mes, programacion__municipio_id=municipios).distinct('programacion') elaboro = CatPersona.objects.get( pk = elaboro) vo = CatPersona.objects.get( pk = vo) listaescuelas= ProgramacionEntregaEscuela.objects.filter(estatus='ACTIVO', programacion__ano=ano,programacion__mes=mes, programacion__municipio_id=municipios) globallechesuelta = 0 globallechepaquete = 0 globalpaquete = 0 diccionariopaqalimentario = {} diccionarioleche = {} diccionariopromotor = {} for p in entrega: entregaescuela = ProgramacionEntregaEscuela.objects.filter(programacion=p.programacion, estatus='ACTIVO', programacion__ano=ano, programacion__mes=mes, programacion__municipio_id=municipios) for escuela in entregaescuela: abasto = RegistroAbasto.objects.get(id=escuela.registro_abasto) if Dotacion.objects.filter(registro_abasto__pk=abasto.id).count() == 0: if 5 <= abasto.totalNinos < 10: resultado = 1 else: resultado = abasto.totalNinos % 10 if resultado == 0: resultado = abasto.totalNinos / 10 elif resultado > 5: resultado = ((abasto.totalNinos / 10) + 1) elif resultado <= 5: resultado = abasto.totalNinos / 10 if abasto.mes == "Abril" or abasto.mes == "Agosto" or abasto.mes == "Diciembre": t_leche = abasto.totalNinosLeche * 2 else: t_leche = abasto.totalNinosLeche * 4 else: dotacion = Dotacion.objects.get(registro_abasto__pk=abasto.id) resultado = dotacion.paquetes t_leche = dotacion.leches if t_leche !=0: if t_leche%12 == 0: tot_ent_leche = (str(t_leche/12)+" paquete(s)") #globallechepaquete=globallechepaquete+(t_leche/12) else: tot_ent_leche = (str(t_leche/12)+" paquete(s) y "+str(t_leche % 12)+" pieza(s)") globallechepaquete=globallechepaquete+(t_leche/12) globallechesuelta=globallechesuelta+(t_leche%12) globalpaquete=globalpaquete+resultado diccionarioleche[escuela.id] = str(tot_ent_leche) diccionariopaqalimentario[escuela.id] = str(resultado) diccionariopromotor[escuela.id]=str(abasto.promotor) entregaa = ProgramacionEntregaEscuela.objects.filter(estatus='ACTIVO', programacion__ano = ano, programacion__mes = mes).distinct('programacion') listaescuelas= ProgramacionEntregaEscuela.objects.filter(estatus='ACTIVO', programacion__ano = ano, programacion__mes = mes) globallechesueltaa = 0 globallechepaquetee = 0 globalpaquetee = 0 diccionariopaqalimentarioo = {} diccionariolechee = {} diccionariopromotor = {} for p in entregaa: entregaescuelaa =ProgramacionEntregaEscuela.objects.filter(programacion = p.programacion, estatus = 'ACTIVO', programacion__ano = ano,programacion__mes = mes) for escuelaa in entregaescuelaa: abastoo = RegistroAbasto.objects.get( id = escuelaa.registro_abasto) if Dotacion.objects.filter(registro_abasto__pk=abastoo.id).count()==0: if abastoo.totalNinos >= 5 and abastoo.totalNinos < 10: resultadoo = 1 else: resultadoo = abastoo.totalNinos % 10 if resultadoo == 0: resultadoo = abastoo.totalNinos / 10 elif resultadoo > 5: resultadoo = ((abastoo.totalNinos / 10) + 1) elif resultadoo <= 5: resultadoo = abastoo.totalNinos / 10 if abastoo.mes == "Abril" or abastoo.mes == "Agosto" or abastoo.mes== "Diciembre": t_lechee = abastoo.totalNinosLeche * 2 else: t_lechee = abastoo.totalNinosLeche * 4 else: dotacionn = Dotacion.objects.get(registro_abasto__pk=abastoo.id) resultadoo = dotacionn.paquetes t_lechee = dotacionn.leches if t_lechee !=0: if t_lechee%12 == 0: tot_ent_lechee = (str(t_lechee/12)+" paquete(s)") #globallechepaquetee=globallechepaquetee+(t_lechee/12) else: tot_ent_lechee = (str(t_lechee/12)+" paquete(s) y "+str(t_lechee%12)+" pieza(s)") globallechepaquetee=globallechepaquetee+(t_lechee/12) globallechesueltaa=globallechesueltaa+(t_lechee%12) globalpaquetee=globalpaquetee+resultadoo diccionariolechee[escuelaa.id] = str(tot_ent_lechee) diccionariopaqalimentarioo[escuelaa.id] = str(resultadoo) #diccionariopromotor[escuela.id]=str(abasto.promotor) contexto = {'entrega': entrega, 'listaescuelas': listaescuelas, 'mes': mes,'ano': ano, 'p_entrega': p_entrega, 'diccionariopromotor': diccionariopromotor, 'diccionariopaqalimentario': diccionariopaqalimentario, 'diccionarioleche': diccionarioleche, 'elaboro': elaboro, 'vo': vo, 'municipios': municipios, 'globallechesueltaa': globallechesueltaa, 'globallechepaquetee': globallechepaquetee, 'globalpaquetee': globalpaquetee} return render_to_response('reporte_programacion_mes_pdf.html', contexto) @login_required(login_url='/login') def reporte_vales_salida_pdf(request): if request.method == 'POST': form = ValesSalidaForm(request.POST) if form.is_valid(): folio = request.POST.get('folio') ano = request.POST.get('ano') mes = request.POST.get('mes') paq = request.POST.get('producto') paquete = CatPaquete.objects.get(tipoPaquete= paq , status='TRUE') cantidad= request.POST.get('cantidad') nopedido = request.POST.get('nopedido') municipio= request.POST.get('municipio') licitacion = request.POST.get('licitacion') nocontrato = request.POST.get('nocontrato') if (nopedido == None): nopedido='' proyecto = CatProyecto.objects.get( municipio = municipio) paquete = CatPaquete.objects.get( pk = paquete.pk) proveedor = CatProveedor.objects.get( pk=paquete.proveedor.pk) municipio = Municipio.objects.get( pk = municipio ) abasto = RegistroAbasto.objects.filter( mes = mes, ano = ano, escuela__localidad__municipio__nombre = municipio.nombre ) total_ninos=0 total_litros_leche= 0 paq_leche = 0 total_paquete = 0 importe_total_paquete_estado = 0 for ninos in abasto: if paquete.tipoPaquete.clave == 'L': if Dotacion.objects.filter(registro_abasto__pk=ninos.id).count()==0: if mes == "Abril" or mes == "Agosto" or mes == "Diciembre": total_ninos = 2* ninos.totalNinosLeche else: total_ninos = 4 * ninos.totalNinosLeche total_litros_leche = total_litros_leche + int(total_ninos) if total_litros_leche % 12 == 0: paq_leche =str (total_litros_leche / 12)+"PAQUETES" else: paq_leche = str(total_litros_leche/12)+" PAQUETES y "+str(total_litros_leche%12)+" BOTES SUELTOS" else: dotacion = Dotacion.objects.get(registro_abasto__pk=ninos.id) resultado=dotacion.leches total_litros_leche=total_litros_leche+resultado if total_litros_leche % 12 == 0: paq_leche =str (total_litros_leche / 12)+"PAQUETES" else: paq_leche = str(total_litros_leche/12)+" PAQUETES y "+str(total_litros_leche%12)+" BOTES SUELTOS" elif paquete.tipoPaquete.clave == 'PA': if Dotacion.objects.filter(registro_abasto__pk=ninos.id).count()==0: if ninos.totalNinos >= 5 and ninos.totalNinos < 10: resultado = 1 else: resultado = ninos.totalNinos % 10 if resultado == 0: resultado = ninos.totalNinos / 10 elif resultado > 5: resultado = ((ninos.totalNinos / 10) + 1) elif resultado <= 5: resultado = ninos.totalNinos / 10 total_paquete=total_paquete+resultado else: dotacion = Dotacion.objects.get(registro_abasto__pk=ninos.id) resultado = dotacion.paquetes total_paquete=total_paquete+resultado if cantidad=='': cantidad='' cant_prod='' elif int(cantidad) % 12 == 0: a=int(cantidad)%12 cant_prod = str (int(cantidad) / 12)+"PAQUETES" else: cant_prod= str (int(cantidad) / 12)+" PAQUETES y "+str(int(cantidad)%12)+" BOTES SUELTOS" contexto = {'proveedor':proveedor,'folio':folio,'nopedido':nopedido , 'ano':ano, 'mes':mes, 'proyecto':proyecto,'paquete':paquete, 'total_litros_leche':total_litros_leche, 'paq_leche':paq_leche,'total_paquete':total_paquete, 'cantidad':cantidad, 'cant_prod':cant_prod, 'licitacion':licitacion,'nocontrato':nocontrato} #contexto = {'proveedor':proveedor,'folio':folio,'nopedido':nopedido , 'ano':ano, 'mes':mes,'proyecto':proyecto,'paquete':paquete, 'total_litros_leche':total_litros_leche, 'paq_leche':paq_leche,'total_paquete':total_paquete, 'cantidad':cantidad, 'cant_prod':cant_prod, 'licitacion':licitacion} return render_to_response("reporte_vales_salida_pdf.html", contexto) else: form = ValesSalidaForm() contexto = {'reg_programacion':'reg_programacion','form':form} return render_to_response('vales_salida.html', contexto, context_instance = RequestContext( request )) @login_required(login_url='/login') def devolucion(request): if request.method=='POST': form = DevolucionForm(request.POST) if form.is_valid(): municipio=request.POST['municipio'] mes=request.POST['mes'] ano=request.POST['ano'] ruta_combo =1 resguardos = ProgramacionEntregaEscuela.objects.filter(escuela__ruta__pk =1,programacion__municipio = municipio,programacion__ano =ano, programacion__mes = mes, estatus='ACTIVO') municipios_rutas_combo = CatRuta.objects.filter(Localidad__municipio=municipio).distinct() return render_to_response('mostrardevolucion.html',{'ruta_combo':ruta_combo,'municipios_rutas_combo':municipios_rutas_combo,'municipio':municipio,'ano':ano,'mes':mes,'resguardos':resguardos},context_instance=RequestContext(request)) else: messages.error(request, 'Corrija los siguientes errores') else: form = DevolucionForm() diccionario={'form':form } return render_to_response('devolucionbusqueda.html',diccionario,context_instance=RequestContext(request)) @login_required(login_url='/login') def estatus_abasto(request,municipio,mes,ano,ruta_combo): municipios_rutas_combo = CatRuta.objects.filter(Localidad__municipio=municipio).distinct() resguardos = ProgramacionEntregaEscuela.objects.filter(escuela__ruta__pk =ruta_combo,programacion__municipio=municipio ,programacion__ano =ano, programacion__mes = mes, estatus='ACTIVO') return render_to_response('mostrardevolucion.html',{'ruta_combo':ruta_combo,'municipios_rutas_combo':municipios_rutas_combo,'ano':ano,'mes':mes,'resguardos':resguardos,'municipio':municipio},context_instance=RequestContext(request)) @api_view(['GET']) @login_required(login_url='/login') def crear_estatus_abasto(request): check_lista = request.GET.getlist('check_lista[]') mes = request.GET.get('mes') ano = request.GET.get('ano') ruta_combo = request.GET.get('ruta_combo') accion = request.GET.get('accion')#1:RESGUARDO,2:DEVOLUCION,3:ENTREGADO mensaje ='' try: for c in check_lista: split=c.split("-") instanciaregistroabasto = RegistroAbasto.objects.get(pk=split[0])#abasto if accion == '1':#RESGUARDO instanciaregistroabasto.enviado=None mensaje='Se marco el abasto como resguardo ' elif accion == '2':#DEVOLUCION if instanciaregistroabasto.mes == mes and instanciaregistroabasto.ano == ano: instanciaregistroabasto.enviado=False else: instanciaregistroabasto.enviado=None mensaje='Se devolvio el abasto' elif accion == '3':#ENTREGADO instanciaregistroabasto.enviado=True mensaje='Se marco el abasto como entregado ' instanciaregistroabasto.save() instanciaprogramacion = ProgramacionEntregaEscuela.objects.get(pk= split[1], estatus= 'ACTIVO')#programacion if accion == '1':#RESGUARDO instanciaprogramacion.estatus='RESGUARDO' elif accion == '2':#DEVOLUCION if instanciaregistroabasto.mes == mes and instanciaregistroabasto.ano == ano: instanciaprogramacion.estatus='DEVOLUCION' else: instanciaprogramacion.estatus='RESGUARDO' elif accion == '3':#ENTREGADO instanciaprogramacion.estatus='ENTREGADO' instanciaprogramacion.save() st = status.HTTP_200_OK data={'status':1,'mensaje':mensaje,'accion':accion} except Exception as e: data={'status':2,'mensaje':'ocurrio un error'} st = status.HTTP_500_INTERNAL_SERVER_ERROR return Response(data, status=st) def devolver(request,idra,pro,m,a): instanciaregistroabasto = RegistroAbasto.objects.get(pk=idra) if instanciaregistroabasto.mes == m and instanciaregistroabasto.ano == a: instanciaregistroabasto.enviado=False instanciaregistroabasto.save() instanciadevolucion = ProgramacionEntregaEscuela.objects.get(pk= pro) instanciadevolucion.estatus='DEVOLUCION' instanciadevolucion.save() resguardos = ProgramacionEntregaEscuela.objects.filter(programacion__municipio=instanciadevolucion.programacion.municipio ,programacion__ano =a, programacion__mes = m, estatus='ACTIVO') #datos = RegistroAbasto.objects.filter(escuela__localidad__municipio__pk=instanciadevolucion.programacion.municipio.id,mes=instanciadevolucion.programacion.mes,ano=instanciadevolucion.programacion.ano,enviado=True) messages.error(request, 'Se devolvio el abasto') #return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'datos':datos,'resguardos':resguardos},context_instance=RequestContext(request)) return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'resguardos':resguardos},context_instance=RequestContext(request)) else: instanciaregistroabasto.enviado=None instanciaregistroabasto.save() instanciadevolucion = ProgramacionEntregaEscuela.objects.get(pk= pro) instanciadevolucion.estatus='RESGUARDO' instanciadevolucion.save() resguardos = ProgramacionEntregaEscuela.objects.filter(programacion__municipio=instanciadevolucion.programacion.municipio ,programacion__ano =a, programacion__mes = m, estatus='ACTIVO') #datos = RegistroAbasto.objects.filter(escuela__localidad__municipio__pk=instanciadevolucion.programacion.municipio.id,mes=instanciadevolucion.programacion.mes,ano=instanciadevolucion.programacion.ano,enviado=True) messages.error(request, 'Se devolvio el abasto') #return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'datos':datos,'resguardos':resguardos},context_instance=RequestContext(request)) return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'resguardos':resguardos},context_instance=RequestContext(request)) def entregado(request,idra,pro,m,a): instanciaregistroabasto = RegistroAbasto.objects.get(pk=idra) instanciaregistroabasto.enviado=True instanciaregistroabasto.save() instanciadevolucion = ProgramacionEntregaEscuela.objects.get(pk= pro, estatus= 'ACTIVO') instanciadevolucion.estatus='ENTREGADO' instanciadevolucion.save() resguardos = ProgramacionEntregaEscuela.objects.filter(programacion__municipio=instanciadevolucion.programacion.municipio ,programacion__ano =a, programacion__mes = m, estatus='ACTIVO') #datos = RegistroAbasto.objects.filter(escuela__localidad__municipio__pk=instanciadevolucion.programacion.municipio.id,mes=instanciadevolucion.programacion.mes,ano=instanciadevolucion.programacion.ano,enviado=True) messages.info(request, 'Se marco el abasto como entregado') #return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'datos':datos,'resguardos':resguardos},context_instance=RequestContext(request)) return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'resguardos':resguardos},context_instance=RequestContext(request)) def resguardo(request,idra,pro,m, a): instanciaregistroabasto = RegistroAbasto.objects.get(pk=idra) instanciaregistroabasto.enviado=None instanciaregistroabasto.save() instanciadevolucion = ProgramacionEntregaEscuela.objects.get(pk=pro,estatus= 'ACTIVO') instanciadevolucion.estatus='RESGUARDO' instanciadevolucion.save() resguardos = ProgramacionEntregaEscuela.objects.filter(programacion__municipio=instanciadevolucion.programacion.municipio ,programacion__ano =a, programacion__mes = m, estatus='ACTIVO') #datos = RegistroAbasto.objects.filter(escuela__localidad__municipio__pk=instanciadevolucion.programacion.municipio.id,mes=instanciadevolucion.programacion.mes,ano=instanciadevolucion.programacion.ano,enviado=True) messages.warning(request, 'Se marco el abasto como resguardo') #return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'datos':datos,'resguardos':resguardos},context_instance=RequestContext(request)) return render_to_response('mostrardevolucion.html',{'ano':a,'mes':m,'resguardos':resguardos},context_instance=RequestContext(request)) @login_required(login_url='/login') def programacion_buscar_e(request): municipios = Municipio.objects.all() contexto= {'municipios':municipios} return render_to_response('buscar_e_programacion.html', contexto, context_instance=RequestContext(request)) # funciĆ³n para Consultar distintos estatus de la programaciĆ³n @login_required(login_url='/login') def estatus_prog(request, municipio, mes, ano,ruta_combo): municipios = Municipio.objects.get(pk=municipio) municipios_rutas_combo = CatRuta.objects.filter(Localidad__municipio=municipio).distinct() municipios_rutas = municipios_rutas_combo.filter(pk=int(ruta_combo)) datos = ProgramacionEntregaEscuela.objects.filter(Q(estatus="ENTREGADO") | Q(estatus="RESGUARDO"), programacion_id__ano=ano,programacion_id__mes=mes, programacion_id__municipio_id=municipio).order_by('escuela') entregado= datos.filter(estatus ="ENTREGADO") resguardo = datos.filter(estatus="RESGUARDO") abasto = RegistroAbasto.objects.filter(Q(enviado='TRUE') | Q(enviado=None), escuela__localidad__municipio = municipio, escuela__ruta__id =ruta_combo ) diccionariototalescuelaentregado ={} diccionariototalescuelaresguardo ={} for ruta in municipios_rutas: totalescuelasentregado=[] totalescuelasresguardo=[] for loc in ruta.Localidad.all(): if loc.municipio.nombre == municipios.nombre: for escuela in loc.catescuela_set.all(): for prog in datos: if ruta.nombre == escuela.ruta.nombre: if escuela.status == True and escuela.id == prog.escuela_id and prog.estatus == "ENTREGADO": totalescuelasentregado.append(escuela) elif escuela.status == True and escuela.id == prog.escuela_id and prog.estatus == "RESGUARDO": totalescuelasresguardo.append(escuela) else: pass if totalescuelasentregado != 0: diccionariototalescuelaentregado[ruta.id]=len(totalescuelasentregado) if totalescuelasresguardo != 0: diccionariototalescuelaresguardo[ruta.id]=len(totalescuelasresguardo) contexto={'municipios_rutas_combo':municipios_rutas_combo,'resguardo': resguardo,'entregado':entregado,'diccionariototalescuelaresguardo':diccionariototalescuelaresguardo,'diccionariototalescuelaentregado':diccionariototalescuelaentregado,'abasto':abasto,'datos': datos, 'mes': mes, 'ano': ano, 'municipios': municipios, 'municipios_rutas': municipios_rutas} return render_to_response('estatus_programacion.html', contexto, context_instance=RequestContext(request)) @login_required(login_url='/login') def busqueda_nivelacion( request ): if request.method == 'POST': form = BusquedaNivelacionForm(request.POST) if form.is_valid(): mes = request.POST.get('mes') ano = request.POST.get('ano') mun = request.POST.get('municipio') municipio = Municipio.objects.get(id = mun) municipios_rutas = CatRuta.objects.filter( Localidad__municipio = municipio ).distinct() validar=None diccionariototalpaquete={} diccionariototalpaqueteleche={} diccionariototapaqueteruta={} diccionariototallecheruta={} diccionariotonelajeruta={} diccionariotonelajecarro={} try: validar = ValidarProgramacion.objects.get( Municipio = municipio, mes = mes, ano = ano) except ValidarProgramacion.DoesNotExist: pass programacionescuela = ProgramacionEntregaEscuela.objects.filter(programacion__municipio =municipio,programacion__mes = mes, programacion__ano = ano).distinct('programacion').exclude(estatus ='DEVOLUCION')#traigo las programaciones que tengan una escuela activa nivelacion = Nivelacion.objects.filter(programacionentrega__municipio = municipio,programacionentrega__mes = mes,programacionentrega__ano = ano) for ruta in municipios_rutas:#traigo las rutas del municipio que la persona haya seleccionado global_total_paquete = 0 global_total_leches = 0 global_tonelaje=0 for p in programacionescuela:#recorro las programaciones que tengas al menos una entrega activa total_leche = 0 total_paquete = 0 if nivelacion: for n in nivelacion: if n.programacionentrega.id == p.programacion.id: total_paquete_nivelacion=0 total_paquete_leche=0 total_tonelaje=0 if p.escuela.ruta.id == ruta.id: paquete = n.total_paquetes leche = n.total_leches total_paquete_nivelacion = total_paquete_nivelacion+paquete total_paquete_leche = total_paquete_leche+leche total_tonelaje = total_tonelaje+n.tonelaje if leche!=0: global_total_leches =global_total_leches+leche#sumatoria de todas los litros de leches por ruta if leche%12==0: total_leche_escuela_ruta=(str(leche/12)+"paquete(s)") else: total_leche_escuela_ruta=(str(leche/12)+" paquete(s) y "+str(leche%12)+" pieza(s)") if global_total_leches%12==0: leche_total =(str(global_total_leches/12)+" paquete(s)") else: leche_total =(str(global_total_leches/12)+" paquete(s)y "+str(global_total_leches%12)+" pieza(s)") diccionariototalpaqueteleche[int(p.programacion.id)]=str(total_leche_escuela_ruta)#diccionario que guarda la conversion de paquete y piezas sueltas por vehiculo diccionariototalpaquete[int(p.programacion.id)]=int(n.total_paquetes) diccionariotonelajecarro[int(p.programacion.id)]=int(n.tonelaje) global_total_paquete = global_total_paquete + total_paquete_nivelacion global_tonelaje = global_tonelaje+total_tonelaje diccionariototallecheruta[int(ruta.id)]=str(leche_total)#diccionario que guarda la conversion de paquete y piezas sueltas por ruta diccionariototapaqueteruta[int (ruta.id) ]= int(global_total_paquete)#obtengo la sumatoria de todos los paquetes por ruta diccionariotonelajeruta[int (ruta.id) ]= int(global_tonelaje)#obtengo la sumatoria de todos los tonelajes por ruta else: if ruta.id == p.escuela.ruta.id: escuelaentrega = ProgramacionEntregaEscuela.objects.filter(programacion=p.programacion.id)#busco las escuelas que tengas esa programacion for escuela in escuelaentrega:#recorro esas escuelas if escuela.escuela.status == True: for registro in escuela.escuela.registroabasto_set.all():#traigo todos los abastos de esa escuela if escuela.registro_abasto == registro.id:#igualo el campo registro_abasto con el id de la tabla registro de abasto if Dotacion.objects.filter(registro_abasto__pk=registro.id).count()==0:#busco si ese registro de abasto tiene dotacion leche = registro.litros_leche() paquete = registro.paquete_alimentario() total_paquete= total_paquete+paquete total_leche = total_leche+leche else: dotacion = Dotacion.objects.get(registro_abasto__pk=registro.id)#si tiene dotacion tomo los valores de la tabla dotacion resultado = dotacion.paquetes resultado2 = dotacion.leches total_paquete=total_paquete+resultado total_leche=total_leche+resultado2 if total_paquete!=0: diccionariototalpaquete[int(p.programacion.id)]=int(total_paquete)#diccionario que contiene la cantidad de paquetes por vehiculo global_total_paquete = global_total_paquete + total_paquete diccionariototapaqueteruta[int (ruta.id) ]= int(global_total_paquete)#obtengo la sumatoria de todos los paquetes por ruta if total_leche!=0: global_total_leches =global_total_leches+total_leche#sumatoria de todas los litros de leches por ruta if total_leche%12==0: total_leche_escuela_ruta =(str(total_leche/12)+" paquete(s)") else: total_leche_escuela_ruta = (str(total_leche/12)+" paquete(s) y "+str(total_leche%12)+" pieza(s)") diccionariototalpaqueteleche[int(p.programacion.id)]=str(total_leche_escuela_ruta)#diccionario que guarda la conversion de paquete y piezas sueltas por vehiculo if global_total_leches%12==0: leche_total =(str(global_total_leches/12)+" paquete(s)") else: leche_total =(str(global_total_leches/12)+" paquete(s)y "+str(global_total_leches%12)+" pieza(s)") diccionariototallecheruta[int(ruta.id)]=str(leche_total)#diccionario que guarda la conversion de paquete y piezas sueltas por ruta try: leche = CatPaquete.objects.get( status = True, tipoPaquete__clave = 'L') paquete = CatPaquete.objects.get( status = True, tipoPaquete__clave = 'PA') pesoporleche = (float(leche.peso) / 12 ) tonelajeleche = (float(pesoporleche) * float(total_leche) ) tonelajepaquete = (float(paquete.peso) * float(total_paquete)) tonelaje =tonelajepaquete + tonelajeleche except CatPaquete.DoesNotExist: pass if tonelaje!=0: diccionariotonelajecarro[int(p.programacion.id)]=int(tonelaje)#diccionario que contiene la cantidad de paquetes por vehiculo global_tonelaje = global_tonelaje + tonelaje diccionariotonelajeruta[int (ruta.id) ]= int(global_tonelaje)#obtengo la sumatoria de todos los paquetes por ruta contexto={'diccionariotonelajecarro':diccionariotonelajecarro,'diccionariotonelajeruta':diccionariotonelajeruta,'nivelacion':nivelacion,'validar':validar,'mes':mes,'ano':ano,'municipio':municipio,'diccionariototalpaquete':diccionariototalpaquete,'diccionariototalpaqueteleche':diccionariototalpaqueteleche,'programacionescuela':programacionescuela, 'municipios_rutas':municipios_rutas,'diccionariototapaqueteruta':diccionariototapaqueteruta,'diccionariototallecheruta':diccionariototallecheruta} return render_to_response('nivelacion.html', contexto, context_instance = RequestContext( request )) else: form = BusquedaNivelacionForm() contexto = {'reg_programacion':'reg_programacion','form':form} return render_to_response('busqueda_nivelacion.html', contexto, context_instance = RequestContext( request )) @login_required(login_url='/login') def nivelacion(request,municipio, mes, ano): municipio = Municipio.objects.get(id = municipio) municipios_rutas = CatRuta.objects.filter( Localidad__municipio = municipio ).distinct() diccionariototapaqueteruta={} diccionariototallecheruta={} diccionariocarroxruta={} try: leche_peso = CatPaquete.objects.get( status = True, tipoPaquete__clave = 'L') paquete_peso = CatPaquete.objects.get( status = True, tipoPaquete__clave = 'PA') pesoporleche = (float(leche_peso.peso) / 12 ) except CatPaquete.DoesNotExist: pass programacionescuela = ProgramacionEntregaEscuela.objects.filter(programacion__municipio =municipio,programacion__mes = mes, programacion__ano = ano,estatus ='ACTIVO' ).distinct('programacion')#traigo las progra for ruta in municipios_rutas:#traigo las rutas del municipio que la persona haya seleccionado carroxruta=0 global_total_paquete = 0 global_total_leches = 0 for p in programacionescuela: if ruta.id == p.escuela.ruta.id: carroxruta = carroxruta+1#sacar cuantos carros estan designados a esa ruta diccionariocarroxruta[int(ruta.id)]=int(carroxruta)#diccionario que contiene los vehiculo por ruta for p in programacionescuela:#recorro las programaciones que tengas al menos una entrega activa total_leche = 0 total_paquete = 0 escuelaentrega = ProgramacionEntregaEscuela.objects.filter(programacion=p.programacion.id, estatus = 'ACTIVO')#busco las escuelas que tengas esa programacion for escuela in escuelaentrega:#recorro esas escuelas if ruta.nombre == escuela.escuela.ruta.nombre: #si la ruta coincide con la ruta de la escuela del ciclo if escuela.escuela.status == True: for registro in escuela.escuela.registroabasto_set.all():#traigo todos los abastos de esa escuela if escuela.registro_abasto == registro.id:#igualo el campo registro_abasto con el id de la tabla registro de abasto if Dotacion.objects.filter(registro_abasto__pk=registro.id).count()==0:#busco si ese registro de abasto tiene dotacion leche = registro.litros_leche() paquete = registro.paquete_alimentario() total_paquete= total_paquete+paquete total_leche = total_leche+leche else: dotacion = Dotacion.objects.get(registro_abasto__pk=registro.id)#si tiene dotacion tomo los valores de la tabla dotacion resultado = dotacion.paquetes resultado2 = dotacion.leches total_paquete=total_paquete+resultado total_leche=total_leche+resultado2 if total_paquete!=0: global_total_paquete = global_total_paquete + total_paquete diccionariototapaqueteruta[int (ruta.id) ]= int(global_total_paquete)#obtengo la sumatoria de todos los paquetes por ruta if total_leche!=0: global_total_leches =global_total_leches+total_leche#sumatoria de todas los litros de leches por ruta diccionariototallecheruta[int(ruta.id)]=int(global_total_leches)#diccionario que guarda los litros de leche por ruta for ruta in municipios_rutas: leche_total_excedente=0 for p in programacionescuela: if ruta.id == p.escuela.ruta.id: for clave,paquete in diccionariototapaqueteruta.iteritems(): if clave == ruta.id: for clave,leche in diccionariototallecheruta.iteritems(): if clave == ruta.id: for clave , carro in diccionariocarroxruta.iteritems(): if clave == ruta.id: if paquete ==0: pass else: if Nivelacion.objects.filter(ruta = ruta.id,programacionentrega__mes =mes, programacionentrega__ano = ano, programacionentrega__municipio = municipio).count()==0:#buscamos si ya hay una nivelacion en esa ruta carro_capacidad = (float(p.programacion.vehiculo.capacidad)*1000) print p.programacion.vehiculo.marca,'carro',carro_capacidad,ruta.id if carro ==1: leche_total=leche//carro#si no encuentra registros, enviaremos los paquetes y leches sueltas en el primer camion de la ruta else: leche_nivelar=leche//carro leche_residuo = leche%carro leche_redondeado = leche_nivelar+leche_residuo leche_suelta = leche_redondeado%12 leche_total = leche_redondeado+leche_suelta leche_tonelaje = (float(pesoporleche)*float(leche_total)) paquete_nivelar= paquete//carro paquete_residuo = paquete%carro#dividimos por el numero de carros que tenga esa ruta paquete_redondeado = paquete_nivelar+paquete_residuo paquete_tonelaje = (float(paquete_peso.peso) * float(paquete_redondeado)) tonelaje = leche_tonelaje + paquete_tonelaje if tonelaje > carro_capacidad: leche_total_antigua = leche_total#guardo los litros de leches dados por la division de la nivelacion, para futuros calculos exceso = tonelaje-carro_capacidad#saco el exceso de peso leche_division = exceso//pesoporleche #convierto ese exceso en litros de leches leche_division_residuo = exceso%pesoporleche#obtengo los litros de leches restantes leche_division_total = leche_division+leche_division_residuo leche_total_resta= leche_total-leche_division_total# a la leche le quito ese exceso transformado ya en litros de leche leche_total_suelta =leche_total_resta%12 leche_total = leche_total_resta -leche_total_suelta#le quito las leches sueltas leche_tonelaje = (float(pesoporleche)*float(leche_total))#sobreescribo el tonelaje de la leche tonelaje = leche_tonelaje + paquete_tonelaje#sobreescribo el tonelaje total try: carro_ruta = Nivelacion.objects.get(ruta =ruta.id,programacionentrega_id__mes = mes,programacionentrega_id__ano= ano,programacionentrega__municipio = municipio).count() except Nivelacion.DoesNotExist: #en caso de no haber un carro asignado a esa ruta , el exceso de paquetes de leche se guardara en una variable para ser sumado en el siguiente camion que venga leche_total_excedente = leche_division_total+leche_total_suelta#le sumo las leches sueltas al exceso nivelar = Nivelacion.objects.create( programacionentrega_id = p.programacion.id, total_leches = leche_total, total_paquetes = paquete_redondeado, ruta_id = ruta.id, tonelaje= tonelaje ) print('---------------------------------------------------------') else:#quiere decir que ya hay un camion en esa ruta, por ende enviaremos sin redondear lo que de la division de los paquetes sobre los carros carro_capacidad = (float(p.programacion.vehiculo.capacidad)*1000) print carro_capacidad,'carro_capacidad' paquete_nivelar=paquete//carro paquete_tonelaje =(float(paquete_peso.peso) * float(paquete_nivelar)) leche_nivelar=leche//carro leche_residuo = leche%carro leche_redondeado = leche_nivelar+leche_residuo leche_suelta = leche_redondeado%12 leche_total = leche_redondeado-leche_suelta#quito las leches sueltas, ya que esas se fueron en el primer carro leche_tonelaje = (float(pesoporleche)*float(leche_total)) tonelaje = leche_tonelaje + paquete_tonelaje total_leche_escuela_ruta=(str(leche_total/12)+" paquete(s) y "+str(leche_total%12)+" pieza(s)") if leche_total_excedente>1:#en caso de encontrar litros de leches de sobra del camion anterior leche_total=leche_total+leche_total_excedente #sobreescribo la variable leche_total ,sumandole los litros de leches excedentes leche_tonelaje = (float(pesoporleche)*float(leche_total))#sobreescribo el tonelaje de la leche tonelaje = leche_tonelaje + paquete_tonelaje #sobreescribo el tonelaje total leche_total_excedente= 0 #reseteo la variable para futuro calculos if tonelaje > carro_capacidad: exceso = tonelaje-carro_capacidad#saco el exceso de peso #leche_total_antigua = leche_total#guardo los litros de leches dados por la division de la nivelacion, para futuros calculos leche_division = exceso//pesoporleche #convierto ese exceso en litros de leches leche_division_residuo = exceso%pesoporleche#obtengo los litros de leches restantes leche_division_total = leche_division+leche_division_residuo leche_peso= (float(pesoporleche)*float(leche_division_total)) leche_total_resta= leche_total-leche_division_total #sobreescribo la variable leche_total, con los nuevos litros de leches leche_total_suelta =leche_total_resta%12 leche_total = leche_total_resta -leche_total_suelta#LE QUTO LAS LECHES SUELTAS leche_tonelaje = (float(pesoporleche)*float(leche_total))#sobreescribo el tonelaje de la leche tonelaje = leche_tonelaje + paquete_tonelaje#sobreescribo el tonelaje total carro_ruta = Nivelacion.objects.filter(ruta =ruta.id,programacionentrega_id__mes = mes,programacionentrega_id__ano= ano,programacionentrega__municipio = municipio) for c in carro_ruta: ton = c.tonelaje+leche_peso carro_capacidad = (float(c.programacionentrega.vehiculo.capacidad)*1000) if ton < carro_capacidad: carro_leche_viejo = c.total_leches carro_tonelaje_viejo = c.tonelaje #leche_division_total_division = leche_division_total%12 leche_division_total = leche_total_suelta+leche_division_total#LE SUMO LAS LECHES SUELTAS carro_leche = c.total_leches+leche_division_total carro_leche_tonelaje=(float(pesoporleche)*float(carro_leche))#sobreescribo el tonelaje de la leche carro_paquete_tonelaje = (float(paquete_peso.peso) * float(c.total_paquetes)) carro_tonelaje = carro_paquete_tonelaje + carro_leche_tonelaje nivelar = Nivelacion( id= c.id, programacionentrega_id = c.programacionentrega.id, total_leches = carro_leche, total_paquetes = c.total_paquetes, ruta_id = c.ruta.id, tonelaje= carro_tonelaje) nivelar.save() else: messages.warning(request, 'Error no hay carro con espacio disponible para la ruta'+str (p.escuela.ruta)) nivelar = Nivelacion.objects.create( programacionentrega_id = p.programacion.id, total_leches = leche_total, total_paquetes = paquete_nivelar, ruta_id = ruta.id, tonelaje= tonelaje ) #contexto={'validar':validar,'mes':mes,'ano':ano,'municipio':municipio,'programacionescuela':programacionescuela, 'municipios_rutas':municipios_rutas,'diccionariototapaqueteruta':diccionariototapaqueteruta,'diccionariototallecheruta':diccionariototallecheruta} #return render_to_response('busqueda_nivelacion.html', context_instance = RequestContext( request )) return HttpResponseRedirect('/busqueda_nivelacion/') @login_required(login_url='/login') def imprimir_nivelacion(request,municipio, mes, ano): municipio = Municipio.objects.get(id = municipio) municipios_rutas = Nivelacion.objects.filter(programacionentrega_id__mes = mes, programacionentrega_id__ano=ano,programacionentrega_id__municipio =municipio).distinct('ruta') diccionariototalpaquete={} diccionariototalpaqueteleche={} diccionariototapaqueteruta={} diccionariototallecheruta={} diccionariotonelaje={} programacionescuela = ProgramacionEntregaEscuela.objects.filter(programacion__municipio =municipio,programacion__mes = mes, programacion__ano = ano, estatus = 'ACTIVO' ).distinct('programacion')#traigo las progra nivelacion = Nivelacion.objects.filter(programacionentrega__municipio = municipio,programacionentrega__mes = mes,programacionentrega__ano = ano) for ruta in municipios_rutas:#traigo las rutas del municipio que la persona haya seleccionado global_total_paquete = 0 global_total_leches = 0 for p in programacionescuela:#recorro las programaciones que tengas al menos una entrega activa total_leche = 0 total_paquete = 0 escuelaentrega = ProgramacionEntregaEscuela.objects.filter(programacion=p.programacion.id)#busco las escuelas que tengas esa programacion if nivelacion: for n in nivelacion: paquete=0 if n.programacionentrega.id == p.programacion.id: total_paquete_nivelacion=0 total_paquete_leche=0 if p.escuela.ruta.id == ruta.ruta.id: paquete = n.total_paquetes leche = n.total_leches total_paquete_nivelacion = total_paquete_nivelacion+paquete total_paquete_leche = total_paquete_leche+leche if leche!=0: global_total_leches =global_total_leches+leche#sumatoria de todas los litros de leches por ruta if leche%12==0: total_leche_escuela_ruta=(str(leche/12)+"paquete(s)") else: total_leche_escuela_ruta=(str(leche/12)+" paquete(s) y "+str(leche%12)+" pieza(s)") if global_total_leches%12==0: leche_total =(str(global_total_leches/12)+" paquete(s)") else: leche_total =(str(global_total_leches/12)+" paquete(s)y "+str(global_total_leches%12)+" pieza(s)") diccionariototalpaqueteleche[int(p.programacion.id)]=str(total_leche_escuela_ruta)#diccionario que guarda la conversion de paquete y piezas sueltas por vehiculo diccionariototalpaquete[int(p.programacion.id)]=int(n.total_paquetes) diccionariotonelaje[int(p.programacion.id)]=int(n.tonelaje) global_total_paquete = global_total_paquete + total_paquete_nivelacion diccionariototallecheruta[int(ruta.ruta.id)]=str(leche_total)#diccionario que guarda la conversion de paquete y piezas sueltas por ruta diccionariototapaqueteruta[int (ruta.ruta.id) ]= int(global_total_paquete)#obtengo la sumatoria de todos los paquetes por ruta contexto={'municipios_rutas':municipios_rutas,'diccionariotonelaje':diccionariotonelaje,'mes':mes,'ano':ano,'municipio':municipio,'diccionariototalpaquete':diccionariototalpaquete,'diccionariototalpaqueteleche':diccionariototalpaqueteleche,'programacionescuela':programacionescuela, 'municipios_rutas':municipios_rutas,'diccionariototapaqueteruta':diccionariototapaqueteruta,'diccionariototallecheruta':diccionariototallecheruta} return render_to_response('reporte_nivelacion.html', contexto, context_instance = RequestContext( request )) @login_required(login_url='/login') def validar_programacion( request, id_municipio, mes, ano ): municipio = Municipio.objects.get(pk = id_municipio) validar = ValidarProgramacion() validar.Municipio = municipio validar.mes = mes validar.ano = ano validar.status = True validar.save() return HttpResponseRedirect('/programacion_entrega/'+id_municipio+' and '+ mes+' and '+ ano) @login_required(login_url='/login') def reporte_insumos( request ): if request.method == 'POST': form = ReporteInsumos(request.POST) if form.is_valid(): mes = request.POST.get('mes') ano = request.POST.get('ano') listamunicipios =Municipio.objects.all() diccionariototallechesprogramadas={} diccionariototalpaquetesprogramadas={} diccionariototalpaquetesentregadas={} diccionariototallechesentregadas={} diccionariototallechesresguardos={} diccionariototalpaquetesresguardos={} diccionariototallechesextras={} diccionariototallechesdisminucion={} diccionariototalpaquetesextras={} diccionariototalpaquetesdisminucion={} globaltotalpaquetesprogramadas=0 globaltotallechesprogramadas=0 globaltotallechesentregadas=0 globaltotalpaquetesentregadas=0 globaltotalpaquetesresguardos=0 globaltotallechesresguardos=0 globaltotallechesextras=0 globaltotallechesdisminucion=0 globaltotalpaqueteextras=0 globaltotalpaquetedisminucion=0 for municipio in listamunicipios: totallechesentregadas=0 totalpaquetesentregadas=0 totallechesresguardo=0 totalpaquetesresguardo=0 totallechesprogramadas=0 totalpaquetesprogramadas=0 dotacionlecheextra=0 dotacionlechedisminucion=0 dotacionpaqueteextra=0 dotacionpaquetedisminucion=0 listaentregado = ProgramacionEntregaEscuela.objects.filter(programacion__mes = mes ,programacion__ano = ano , programacion__municipio = municipio, estatus ='ENTREGADO') registroabasto = RegistroAbasto.objects.filter(escuela__localidad__municipio = municipio, mes = mes , ano = ano)#triago los registros de abasto del municipio en turno for registro in registroabasto: totalpaquetesprogramadas = totalpaquetesprogramadas+registro.paquete_alimentario() totallechesprogramadas=totallechesprogramadas+registro.litros_leche() for entregado in listaentregado: if registro.id == entregado.registro_abasto: totallechesentregadas=totallechesentregadas+ registro.litros_leche() totalpaquetesentregadas = totalpaquetesentregadas+registro.paquete_alimentario() if registro.enviado == None: totallechesresguardo = totallechesresguardo+registro.litros_leche() totalpaquetesresguardo = totalpaquetesresguardo+registro.paquete_alimentario() if Dotacion.objects.filter(registro_abasto__pk=registro.id).count()!=0: dotacion = Dotacion.objects.get(registro_abasto__pk = registro.id) if (mes == "Abril" or mes == "Agosto" or mes == "Diciembre"): lechetotal = int(registro.totalNinosLeche * 2) else: lechetotal = (registro.totalNinosLeche * 4) if dotacion.leches > lechetotal: dotacionlecheextra = dotacionlecheextra + (dotacion.leches - lechetotal) elif dotacion.leches < lechetotal: dotacionlechedisminucion = dotacionlechedisminucion + (lechetotal-dotacion.leches) if registro.totalNinos >= 5 and registro.totalNinos < 10: resultado = 1 else: resultado = registro.totalNinos % 10 if resultado == 0: resultado = registro.totalNinos / 10 elif resultado > 5: resultado = ((registro.totalNinos / 10) + 1) elif resultado <= 5: resultado = registro.totalNinos / 10 paquetetotal=resultado if dotacion.paquetes > paquetetotal: dotacionpaqueteextra = dotacionpaqueteextra + (dotacion.paquetes - paquetetotal) elif dotacion.paquetes < paquetetotal: dotacionpaquetedisminucion = dotacionpaquetedisminucion+ ( paquetetotal-dotacion.paquetes ) diccionariototalpaquetesentregadas[int (municipio.id)]=int(totalpaquetesentregadas) diccionariototallechesentregadas[int (municipio.id)]=int(totallechesentregadas) globaltotallechesentregadas=globaltotallechesentregadas+totallechesentregadas globaltotalpaquetesentregadas=globaltotalpaquetesentregadas+totalpaquetesentregadas diccionariototallechesprogramadas [int (municipio.id)]=int(totallechesprogramadas) diccionariototalpaquetesprogramadas [int (municipio.id)]=int(totalpaquetesprogramadas) globaltotalpaquetesprogramadas = globaltotalpaquetesprogramadas+totalpaquetesprogramadas globaltotallechesprogramadas=globaltotallechesprogramadas+totallechesprogramadas diccionariototallechesresguardos [int (municipio.id)]=int(totallechesresguardo) diccionariototalpaquetesresguardos [int (municipio.id)]=int(totalpaquetesresguardo) globaltotalpaquetesresguardos =globaltotalpaquetesresguardos+totalpaquetesresguardo globaltotallechesresguardos =globaltotallechesresguardos+totallechesresguardo diccionariototalpaquetesextras [int (municipio.id)]=int(dotacionpaqueteextra) diccionariototalpaquetesdisminucion [int (municipio.id)]=int(dotacionpaquetedisminucion) globaltotalpaqueteextras =globaltotalpaqueteextras+dotacionpaqueteextra globaltotalpaquetedisminucion =globaltotalpaquetedisminucion+dotacionpaquetedisminucion diccionariototallechesextras [int (municipio.id)]=int(dotacionlecheextra) diccionariototallechesdisminucion [int (municipio.id)]=int(dotacionlechedisminucion) globaltotallechesextras =globaltotallechesextras+dotacionlecheextra globaltotallechesdisminucion =globaltotallechesdisminucion+dotacionlechedisminucion contexto={'diccionariototallechesextras':diccionariototallechesextras,'diccionariototallechesdisminucion':diccionariototallechesdisminucion,'globaltotallechesresguardos':globaltotallechesresguardos,'globaltotalpaquetesresguardos':globaltotalpaquetesresguardos,'globaltotalpaquetesentregadas':globaltotalpaquetesentregadas,'globaltotallechesentregadas':globaltotallechesentregadas,'globaltotallechesprogramadas':globaltotallechesprogramadas,'globaltotalpaquetesprogramadas':globaltotalpaquetesprogramadas, 'diccionariototalpaquetesresguardos':diccionariototalpaquetesresguardos,'diccionariototallechesresguardos':diccionariototallechesresguardos,'diccionariototalpaquetesentregadas':diccionariototalpaquetesentregadas,'diccionariototallechesentregadas':diccionariototallechesentregadas,'diccionariototalpaquetesprogramadas':diccionariototalpaquetesprogramadas,'diccionariototallechesprogramadas':diccionariototallechesprogramadas,'listamunicipios':listamunicipios,'mes':mes,'ano':ano, 'globaltotalpaquetedisminucion':globaltotalpaquetedisminucion,'globaltotalpaqueteextras':globaltotalpaqueteextras,'diccionariototalpaquetesdisminucion':diccionariototalpaquetesdisminucion,'diccionariototalpaquetesextras':diccionariototalpaquetesextras,'globaltotallechesdisminucion':globaltotallechesdisminucion,'globaltotallechesextras':globaltotallechesextras,'reg_programacion':'active'} return render_to_response('reporte_insumos.html', contexto, context_instance = RequestContext( request )) else: form = ReporteInsumos() contexto = {'reg_programacion':'active','form':form} return render_to_response('busqueda_insumos.html', contexto, context_instance = RequestContext( request ))