a
    ;Xh6                     @   s|  d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d dl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2Z2d dl3m4Z4 dd Z5dd Z6dd Z7dd Z8dd  Z9d!d" Z:d d#l;m<Z< d dl=Z=d$d% Z>dS )&    )renderredirectget_object_or_404)get_user_info)EspeciesForms)messages)Especies)PlantioOutrasInformacoesModels)ProducaoMudas)ObjectDoesNotExist)CadastroLote	Semeadura)Repique)EstufaSelecao)ProtocoloSemeadura)letter)colors)SimpleDocTemplate	ParagraphSpacerTable
TableStyleImage	PageBreak)getSampleStyleSheet)inch)BytesIO)HttpResponse)settingsN)	mark_safec                 C   s,   | j jstdS t| j j}t| dd|iS )Nindexzespecies/cadastro_especies.html	user_info)useris_authenticatedr   r   idr   )requestr"    r'   7/var/www/viveiro_mudafortebrasil/apps/especies/views.pycadastro_especies   s    r)   c                 C   s   t  }| jjstdS t| jj}| jdkrt | j| j}|	 r|j
d }tjj|d rpt| td n |  t| td tdS t| d||d	S )
Nr!   POSTespecies)r+   ue   <i class="fa-solid fa-circle-exclamation"></i><strong class="mx-1">Erro!</strong> Espécie ja existe.uo   <i class="fa-solid fa-circle-check"></i><strong class="mx-1">Sucesso!</strong>  Espécie cadastrada com êxito.lista_especiesz"especies/cadastro_de_especies.html)formr"   )r   r#   r$   r   r   r%   methodr*   FILESis_validcleaned_datar   objectsfilterexistsr   errorr    savesuccessr   )r&   r-   r"   r+   r'   r'   r(   cadastro_de_especies"   s$    

r8   c                 C   s   | j jstdS t| j j}tj }tj	}|D ]^}|j
rL| |j
 |_
ntj d|_
tjj|d |_tjj|d |_t|j
|_q0t| d||dS )Nr!   zassets/img/foto_planta.gif)especiezespecies/lista_especies.html)Zcardsr"   )r#   r$   r   r   r%   r   r2   allr   	MEDIA_URLfoto_especie
STATIC_URLr
   r3   r4   Zproducao_existenter	   Zinformacao_existenteboolZimagem_existenter   )r&   r"   r+   base_urlr9   r'   r'   r(   r,   =   s"    
r,   c                 C   s   t t|d}| jjstdS t| jj}| jdkrt| j	| j
|d}| rd| j
v rb| j
d |_|  t| td tdS n
t|d}t| d||||d	S )
N)pkr!   r*   )instancer<   uo   <i class="fa-solid fa-circle-check"></i><strong class="mx-1">Sucesso!</strong>  Espécie atualizada com êxito.r,   zespecies/editar_especie.html)r-   r"   r9   
especie_id)r   r   r#   r$   r   r   r%   r.   r   r*   r/   r0   r<   r6   r   r7   r    r   )r&   rB   r9   r"   r-   r'   r'   r(   editar_especie[   s     



rC   c                 C   s`   z0t jj|d}|  t| td tdW S  tyZ   t	| td td Y S 0 d S )Nr%   ul   <i class="fa-solid fa-circle-check"></i><strong class="mx-1">Sucesso!</strong> Espécie excluida com êxito.r,   u   <i class="fa-solid fa-circle-exclamation"></i><strong class="mx-1">Erro!</strong> Ocorreu um problema ao tentar excluir a Espécie.)
r   r2   getdeleter   r7   r    r   r   r5   )r&   rB   r+   r'   r'   r(   deletar_especiesu   s    
rG   c                 C   s  t j }tdd}d|d< t }t|td}g }tj	t
jd}t|dt dt d	}g }|| d
}	t|	t d }
g }||
 ||gg}t|dt dt gd}|tddg || |tddt  g }|g d |D ]}||j|j|j|jg qd}t|}tddd|fdddtjfddddddtjfddddtjfg}|| || d}t|t d }|tddt  || || |d ||  |   |S ) Nzapplication/pdf)content_typez'attachment; filename=lista_especies.pdfzContent-Disposition)Zpagesizezassets/img/logo-footer.jpgg      @   )widthheightu   
    Telefone: (73) 2011-0214
    Email: ama@viveiroama.com.br
    Endereço: Rod Br 101 KM 888 s/n Zona Rural (ao lado da parada do pedrao), Teixeira de Freitas, BA, Brazil
    ZNormal   )Z	colWidths)ALIGNr   r   rP   CENTER)ZVALIGNrN   rO   rQ      g      ?)ZNomezNome PopularZFamiliazRisco Extincaoz#198754Z
BACKGROUNDrN   rP   r   Z	TEXTCOLOR)rM   rN   )rP   LEFT)ZFONTNAMErN   rS   zHelvetica-Bold)ZBOTTOMPADDINGrN   rS      )r   rR   rO   ZGRIDu4   Informações adicionais no rodapé, se necessário.r   )!r   r2   r:   r   r   r   r   ospathjoinr   STATIC_ROOTr   r   appendr   r   r   ZsetStyler   r   r+   nome_popularfamiliarisco_extincaor   Z
whitesmokeblackbuildseekwritereadclose)r&   ZdadosresponsebufferdocelementsZ	logo_pathZlogoZlogo_contentZempresa_infoZempresa_info_paragraphZempresa_info_contentZ
table_datatabledatar9   Z	hex_colorstyleZrodape_infoZrodape_info_paragraphr'   r'   r(   generate_pdf   sr    







	





rl   )defaultdictc                 C   s  | j jstdS t| j j}tj }d }d}d}d}d}d}d}	d}
g }g }g }g }g }| jdkr| j	
d}|s| rt|}n|rtjj|d }|r|jddddd	d |_|jddddd	d |_|jddddd	d |_|jddddd	d |_tjj|jd
 }tjj|jddd}tdd }|D ]R}|jj}|| d  |jpd7  < || d  |jpd|jpd 7  < qrdd | D }t dd |D }t dd |D }t!jj|jd"dddddddd}t d d |D }t#jj|jd"d!d"d#d$d}t d%d |D }t$jj|jd"d"d&d'd}t d(d |D }
t%jj|jd"d"d)d*d}t d+d |D }	t&| d,|||||||||
|	|pd ddd-g||||d.S )/Nr!   r   GETespecie_selecionadarD   [ ]')rB   )Zsemeadura__lote__especie_id	protocolo	semeadurac                   S   s
   dddS )Nr   )
quantidadevalor_monetarior'   r'   r'   r'   r(   <lambda>	      z$relatorio_especies.<locals>.<lambda>rv   rw   c                 S   s$   g | ]\}}||d  |d dqS )rv   rw   rj   rv   rw   r'   ).0rj   Zvaloresr'   r'   r(   
<listcomp>  s   z&relatorio_especies.<locals>.<listcomp>c                 s   s   | ]}|d  V  qdS )rv   Nr'   r{   itemr'   r'   r(   	<genexpr>  ry   z%relatorio_especies.<locals>.<genexpr>c                 s   s   | ]}|d  V  qdS )rw   Nr'   r}   r'   r'   r(   r     ry   )Zlote__especie_idlote__numero_lotenumero_mudadata_semeiametodo_semeaduralocal_semeaduraaberturaqtd_sementes_utilizadaZregistrado_por__usernamec                 s   s   | ]}|d  V  qdS )r   Nr'   r{   sr'   r'   r(   r   $  ry   semeadura__numero_mudasemeadura__lote__numero_lotedata_repiquequantidade_repicadac                 s   s   | ]}|d  V  qdS )r   Nr'   )r{   rr'   r'   r(   r   0  ry   data_estufaquantidade_mortalidadec                 s   s   | ]}|d  V  qdS )r   Nr'   )r{   er'   r'   r(   r   ;  ry   data_selecaoquantidade_selecaoc                 s   s   | ]}|d  V  qdS )r   Nr'   r   r'   r'   r(   r   F  ry   z especies/relatorio_especies.htmlrz   )r+   r"   ro   total_lotestotal_mudas_vendidastotal_valor_monetariototal_qtd_utilizadatotal_qtd_repicadatotal_qtd_mortalidadetotal_qtd_selecaovendas_por_datasemeaduras_por_especierepiques_por_especieestufas_por_especieselecoes_por_especie)'r#   r$   r   r   r%   r   r2   r:   r.   rn   rE   r4   randomchoicer3   firstbiomareplacestripformacao_vegetalfitofisionomiaspresenca_estadosr   countr   select_relatedrm   rt   data_criacaoqtd_especie
valor_unititemssumr   valuesr   r   r   r   )r&   r"   r+   ro   r   r   r   r   r   r   r   r   r   r   r   r   Zespecie_selecionada_idZprotocolos_semeadurasZdados_agrupadospsr   r'   r'   r(   relatorio_especies   s    
$$$$,	r   )?django.shortcutsr   r   r   apps.dashboard.utilsr   Zapps.especies.formsr   django.contribr   Zapps.especies.modelsr   Zapps.outras_informacoes.modelsr	   Zapps.producao.modelsr
   django.core.exceptionsr   apps.lotes.modelsr   r   apps.repique.modelsr   apps.estufa.modelsr   r   Zapps.protocolos.modelsr   Zreportlab.lib.pagesizesr   Zreportlab.libr   Zreportlab.platypusr   r   r   r   r   r   r   Zreportlab.lib.stylesr   Zreportlab.lib.unitsr   ior   django.httpr   django.confr   rW   django.utils.safestringr    r)   r8   r,   rC   rG   rl   collectionsrm   r   r   r'   r'   r'   r(   <module>   s<   $Y