o
    iS3                     @   s   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
 d dlmZ d dlmZ d dlmZ d d	lmZ G d
d dejZG dd dejZG dd dejZd dlmZ G dd dejZG dd dejZdS )    )serializers)oportunidad_de_venta_model)ColumnWorkflowOportunidadDeVenta)Empresa)Contacto)Usuario)EstadoOportunidadVenta)Origen)OportunidadProductoSerializerc                   @   s   e Zd Ze ZejdddZejdddZ	ejdddZ
ejdddZejdddZejdddZe Ze Ze Zejd	ddZeddd
ZG dd dZdd Zdd Zdd Zdd ZdS )OportunidadDeVentaSerializerzcolumn_workflow.workflow_idT)source	read_onlyzcolumn_workflow.nombrezempresa.nombrezempresa.nombre_comercialzcontacto.nombrez estado_oportunidad.nombre_estadozcolumn_workflow.id)manyr   c                   @   s   e Zd ZeZg dZdS )z!OportunidadDeVentaSerializer.Meta)idnombre
empresa_idempresa_nombrenombre_comercialcontacto_idcontacto_nombreusuario_responsable_idusuario_responsable_nombreestado_oportunidad_idestado_oportunidad_nombrevalor_estimadofecha_estimada_cierreorigenorigen_nombrefecha_creacionfecha_ultima_actualizacion
deleted_atworkflow_nombrenombre_workflowworkflow_idcolumn_workflowcolumn_workflow_id	productosN)__name__
__module____qualname__r   modelfields r-   r-   =/var/www/html/crm_dreinet/oportunidad_de_venta/serializers.pyMeta   s    r/   c                 C   s   |j rCt|j drt|j dr|j j d|j j  S t|j dr5t|j dr5|j j d|j j  S t|j dr?|j jS |j jS d S )Nr   apellido 
first_name	last_name)usuario_responsablehasattrr   r0   stripr2   r3   usernameselfobjr-   r-   r.   get_usuario_responsable_nombre:   s   z;OportunidadDeVentaSerializer.get_usuario_responsable_nombrec                 C   s2   |j sd S tjj|j jddd }|r|jS d S )N   T)r$   ordendeleted_at__isnull)r%   r   objectsfilterr$   firstr   )r9   r:   columna_orden1r-   r-   r.   get_workflow_nombreF   s   z0OportunidadDeVentaSerializer.get_workflow_nombrec                 C   sH   |j sd S ddlm} z|jj|j jd}|jW S  |jy#   Y d S w )Nr   )Workflowr   )r%   Models.workflowrD   r?   getr$   r   DoesNotExist)r9   r:   rD   wfr-   r-   r.   get_nombre_workflowR   s   z0OportunidadDeVentaSerializer.get_nombre_workflowc                 C   s   |j r-zt|j  rtjj|j d}|jW S |j W S  tjtt	fy,   t|j  Y S w |j
rcz#|jrK|jjrN|jjjrQtjj|jjjd}|jW S W d S W d S W d S  tjtttfyb   Y d S w d S )NrE   )r   strisdigitr
   r?   rG   r   rH   
ValueError	TypeErrorr   contactoempresa	origen_idAttributeError	Exception)r9   r:   
origen_objr-   r-   r.   get_origen_nombre\   s4   
z.OportunidadDeVentaSerializer.get_origen_nombreN)r(   r)   r*   r   SerializerMethodFieldr"   IntegerFieldr$   	CharFieldr%   r   r   r   r   r   r   r#   r&   r   r'   r/   r;   rC   rJ   rU   r-   r-   r-   r.   r      s$    
r   c                   @   s   e Zd ZejdddZejdddZejddddZejdddZ	ejdd	dZ
ejd
dddddZejdddZejddddZejej dddddZeje dddZG dd dZdd Zdd Zdd ZdS ) CreateOportunidadSerializerTDescripcion  de ventarequired	help_text1ID de la empresa relacionada con esta oportunidadFz0ID del contacto relacionado con esta oportunidad)r\   
allow_nullr]   .ID del usuario responsable de esta oportunidadID del estado de la oportunidad      /Valor estimado de la oportunidad (ej: 15000.00)
max_digitsdecimal_placesr\   r_   r]   .Fecha estimada de cierre (formato: YYYY-MM-DD)d   :Origen de la oportunidad (ej: Referido, Web, Llamada, etc)
max_lengthr\   r]   r%   z4ID del workflow asignado a la oportunidad (opcional))querysetr   r\   r_   r]   zOLista de productos [{'producto_id': 1, 'cantidad': 2, 'precio_unitario': 10.0}])childr\   r]   c                   @      e Zd ZejZg dZdS )z CreateOportunidadSerializer.Meta)
r   r   r   r   r   r   r   r   r&   r'   Nr(   r)   r*   r   r   r+   r,   r-   r-   r-   r.   r/          r/   c                 C   2   zt jj|dd W |S  t jy   tdw NTr   r>   z!La empresa especificada no exister   r?   rG   rH   r   ValidationErrorr9   valuer-   r-   r.   validate_empresa_id      
z/CreateOportunidadSerializer.validate_empresa_idc                 C   rr   NTrt   z-El usuario responsable especificado no exister   r?   rG   rH   r   rv   rw   r-   r-   r.   validate_usuario_responsable_id   rz   z;CreateOportunidadSerializer.validate_usuario_responsable_idc              
   C   s   z
t jj|d}|W S  t jy9   t j }dd |d d D }d| d}|r4|dd| 7 }t| tyN } z
td	t	| d }~ww )
NrE   c                 S   s    g | ]}d |j  d|j qS )zID: z
, Nombre: )r   nombre_estado).0er-   r-   r.   
<listcomp>   s     zNCreateOportunidadSerializer.validate_estado_oportunidad_id.<locals>.<listcomp>   z El estado de oportunidad con ID z  no existe en la base de datos. zEstados disponibles: z, +Error al validar el estado de oportunidad: )
r	   r?   rG   rH   alljoinr   rv   rS   rK   )r9   rx   estadoestados_disponiblesestados_infomensajer   r-   r-   r.   validate_estado_oportunidad_id   s   

z:CreateOportunidadSerializer.validate_estado_oportunidad_idN)r(   r)   r*   r   rX   r   rW   r   r   r   r   DecimalFieldr   	DateFieldr   r   PrimaryKeyRelatedFieldr   r?   r   r&   	ListField	DictFieldr'   r/   ry   r}   r   r-   r-   r-   r.   rY   x   s2    rY   c                   @   s   e Zd ZejdddZejdddZeje ddZ	dd Z
ejdd	dZejdd
dZejdddZejdddZejddddddZejdddZejddddZejdddZG dd dZdd Zdd Zdd Zdd Zd S )!EditOportunidadSerializerFzID del workflow asociador[   Tz&ID de la oportunidad de venta a editar)rn   r\   c                 C   s:   zt jj|dd}|W S  t jy   td| dw )NTrt   zLa oportunidad de venta con ID z no existe o ya fue eliminada)r   r?   rG   rH   r   rv   )r9   rx   oportunidadr-   r-   r.   validate_id   s   z%EditOportunidadSerializer.validate_idrZ   r^   r`   ra   rb   rc   rd   re   rh   ri   rj   rk   z)ID del workflow asociado a la oportunidadc                   @   ro   )zEditOportunidadSerializer.Meta)r   r   r   r   r   r   r   r   r   r$   r&   r'   Nrp   r-   r-   r-   r.   r/      rq   r/   c                 C   2   zt jj|dd |W S  t jy   tdw rs   ru   rw   r-   r-   r.   ry         
z-EditOportunidadSerializer.validate_empresa_idc                 C   r   r{   r|   rw   r-   r-   r.   r}      r   z9EditOportunidadSerializer.validate_usuario_responsable_idc              
   C   sZ   z
t jj|d}|W S  t jy   td ty, } z
tdt| d }~ww )NrE   zEstado no exister   )r	   r?   rG   rH   r   rv   rS   rK   )r9   rx   r   r   r-   r-   r.   r      s   
z8EditOportunidadSerializer.validate_estado_oportunidad_idc                 C   s0   z
t jj|d |W S  t jy   tdw )NrE   z"El workflow especificado no existe)r   r?   rG   rH   r   rv   rw   r-   r-   r.   validate_workflow_id   s   
z.EditOportunidadSerializer.validate_workflow_idN)r(   r)   r*   r   rW   r$   r   r   r   r'   r   rX   r   r   r   r   r   r   r   r   r   r&   r/   ry   r}   r   r   r-   r-   r-   r.   r      s*    	r   )
Incidenciac                   @   sB   e Zd Ze ZejddZejddZG dd dZ	dd Z
dS )	IncidenciaSerializerztipo_incidencia.nombre_tipo)r   zestado_incidencia.nombre_estadoc                   @   s    e Zd ZeZg dZg dZdS )zIncidenciaSerializer.Meta)r   titulodescripcionrP   rO   usuariousuario_nombretipo_incidenciatipo_nombreestado_incidenciaestado_nombrer   r   r    )r   r   r    r   N)r(   r)   r*   r   r+   r,   read_only_fieldsr-   r-   r-   r.   r/   
  s    r/   c                 C   s   |j r|j jp|j jpdS dS )Nr   Desconocido)r   r   emailr8   r-   r-   r.   get_usuario_nombre  s   z'IncidenciaSerializer.get_usuario_nombreN)r(   r)   r*   r   rV   r   ReadOnlyFieldr   r   r/   r   r-   r-   r-   r.   r     s    
r   c                   @   s   e Zd ZejdddZdS )DeleteOportunidadSerializerTz(ID de la oportunidad de venta a eliminarr[   N)r(   r)   r*   r   rW   r   r-   r-   r-   r.   r     s
    
r   N)rest_frameworkr   Modelsr   !Models.oportunidad_de_venta_modelr   r   Models.Empresar   Models.contacto_modelr   api_login.modelsr   Models.estado_oportunidad_ventar	   Models.origenr
   producto.serializersr   ModelSerializerr   rY   r   Models.incidencia_modelr   r   
Serializerr   r-   r-   r-   r.   <module>   s    mBI