o
    >:hN                     @   sl  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mZ d dlmZ d dlmZ e ZG dd dejZG dd dejZG dd dejZG dd dejZ G dd dejZ!G dd dejZ"G dd dejZ#G dd dejZ$G dd dejZ%G dd  d ejZ&G d!d" d"ejZ'd#S )$    )models)ValidationError)MinValueValidator)timezone)Decimal)	ProveedorProveedorReferencia)
Referencia)	FormaPagoEmpaqueCompra)get_user_model)PlantaProduccionMaquina)post_delete)receiverc                       s   e Zd Zg dZejeejddZeje	ejddZ
ejddZejddZejeejd	d
ZejdeddZejddZejdddddZG dd dZdd Z fddZedd Zdd Zdd Zdd Z  ZS ) OrdenCompra))borradorBorrador)	pendienteu   Pendiente de aprobación)aprobadaAprobada)	rechazada	Rechazada)	en_caminoz	En camino)recibidaRecibida)	cancelada	Canceladaordenes_compra	on_deleterelated_nameordenes_compra_creadasT)auto_now_addzFecha requerida de entrega)verbose_namezForma de pagor    r$      r   )
max_lengthchoicesdefaultblankFu   Número de orden)r'   uniqueeditabler$   c                   @   s&   e Zd ZdZdZdZdgZddgZdS )zOrdenCompra.Metacompras_ordencomprazOrden de Comprau   Órdenes de Compraz-fecha_creacion)aprobar_ordencomprau    Puede aprobar órdenes de compra)recibir_ordencomprau$   Puede marcar órdenes como recibidasN)__name__
__module____qualname__db_tabler$   verbose_name_pluralorderingpermissions r8   r8   */var/www/html/public/movimientos/models.pyMeta8   s    r:   c                 C   s   d| j  d| j d| j dS )NOC-z | z ())numero_orden	proveedorestadoselfr8   r8   r9   __str__B      zOrdenCompra.__str__c                    s   | j s"t d}tjjd| d }d| d|d d| _ | jdkrr| j	j
du rrtjj| jd	}|jdkrr| j D ]2}|j}|j}|jpKd
}	|j}
|j}||
 }|d
kre||	 |
|  | }n|}||_||_|  q?t j|i | d S )Nz%Y%m%dr;   )numero_orden__startswith-   04dr   Fpkr   )r=   r   nowstrftimer   objectsfiltercountr?   _stateaddinggetrI   itemsall
referenciastockprecio_costocantidadprecio_unitariosavesuper)rA   argskwargsfechaultima_ordenoriginalitemrefcantidad_existentecosto_existentecantidad_nuevacosto_nuevonuevo_stocknuevo_costo	__class__r8   r9   rY   E   s:   


zOrdenCompra.savec                 C   s   t dd | j D S )Nc                 s   s    | ]}|j V  qd S N)subtotal).0r`   r8   r8   r9   	<genexpr>j   s    z$OrdenCompra.total.<locals>.<genexpr>)sumrR   rS   r@   r8   r8   r9   totalh   s   zOrdenCompra.totalc                 C   s,   | j dv r| jt  k rtdd S d S )N)r   r   z-La fecha requerida no puede ser en el pasado.)r?   fecha_requeridar   rJ   dater   r@   r8   r8   r9   cleanl   s   zOrdenCompra.cleanc                 C   $   | j dkr	tdd| _ |   d S )Nr   u+   Solo se pueden aprobar órdenes pendientes.r   r?   r   rY   rA   usuarior8   r8   r9   aprobarp      
zOrdenCompra.aprobarc                 C   rs   )Nr   u*   Solo se pueden recibir órdenes en camino.r   rt   ru   r8   r8   r9   marcar_como_recibidav   rx   z OrdenCompra.marcar_como_recibida)r1   r2   r3   ESTADOSr   
ForeignKeyr   PROTECTr>   User
creada_porDateTimeFieldfecha_creacion	DateFieldrp   r
   
forma_pago	CharFieldr?   	TextFieldobservacionesr=   r:   rB   rY   propertyro   rr   rw   ry   __classcell__r8   r8   rh   r9   r      sN    

#
r   c                       s   e Zd ZejeejddZejeej	ddZ
ejddedgdZejddedgdZejeej	d	d
Zejeej	ddddZejddddZejdddZG dd dZedd Z fddZdd Zdd Z  ZS )ItemOrdenComprarR   r   items_orden_compra      g-C6?
max_digitsdecimal_places
validatorsr   zEmpaque de comprar%   Tu   Relación Proveedor-Referencia)r    nullr+   r$   u"   Tiempo estimado de entrega (días))r$   r   r+   FzCertificado por proveedor)r)   r$   c                   @   s,   e Zd ZdZdZdZejddgddgZdS )	zItemOrdenCompra.Metacompras_itemordencomprau   Ítem de Orden de Comprau   Ítems de Órdenes de CompraordenrT   unique_item_orden_referencia)fieldsnameN)	r1   r2   r3   r4   r$   r5   r   UniqueConstraintconstraintsr8   r8   r8   r9   r:      s    r:   c                 C   s$   | j d u s
| jd u rdS | j | j S Nr   )rW   rX   r@   r8   r8   r9   rk      s   zItemOrdenCompra.subtotalc                    s8   | j d u r| jr| jj r| jj | _ t j|i | d S rj   )	lead_timeproveedor_referenciarZ   rY   rA   r[   r\   rh   r8   r9   rY      s   
zItemOrdenCompra.savec                 C   s4   | j d ur| j nd}| jpd d| jj d| dS )NzNo definidor   z x z ($z/u))rX   rW   rT   sku)rA   precior8   r8   r9   rB      s    zItemOrdenCompra.__str__c                 C   s&   t jj| jj| jd stdd S )N)r>   rT   z8Este proveedor no suministra la referencia seleccionada.)r   rL   rM   r   r>   rT   existsr   r@   r8   r8   r9   rr      s   zItemOrdenCompra.clean)r1   r2   r3   r   r{   r   CASCADEr   r	   r|   rT   DecimalFieldr   rW   rX   r   empaquer   r   PositiveIntegerFieldr   BooleanFieldcertificador:   r   rk   rY   rB   rr   r   r8   r8   rh   r9   r   |   s`    
r   c                       s   e Zd ZejeejdZeje	j
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dZejdd	ddZ fd
dZdd Z  ZS )AjusteInventarioFisicor    r)   r   r   r   r   r   )r   r   r)         c                    sr   | j s| jj| _t| j| j d| _t| j| j d| _| j| jj | _	| j| j_| j
  t j
|i | d S r   )rI   rT   rU   cantidad_antesmaxcantidad_despuesunidades_entradaunidades_salidarV   valor_totalrY   rZ   r   rh   r8   r9   rY      s   


zAjusteInventarioFisico.savec                 C      d| j j d| j S )NzAjuste inventario  - rT   r   r]   r@   r8   r8   r9   rB         zAjusteInventarioFisico.__str__)r1   r2   r3   r   r{   r	   r|   rT   r   r   rJ   r]   r   r   r   r   r   r   rY   rB   r   r8   r8   rh   r9   r      s    
r   c                       s   e Zd ZejeejdZeje	j
dZejdddZejddZejeejdZejdddd	Zejdddd	Zejdddd	Zejd
ddd	ZG dd dZ fddZdd Z  ZS )MuestraComercialr   r   r   r   r   Tr*   Fr   r   r-   r   r   c                   @      e Zd ZdZdZdgZdS )zMuestraComercial.MetazMuestra ComercialzMuestras Comerciales-fechaNr1   r2   r3   r$   r5   r6   r8   r8   r8   r9   r:          
r:   c                    sf   | j s(| jj| _| jj| _| j| j | _| j| j | _| j| j_| jj	dgd t
 j	|i | d S NrU   update_fields)rI   rT   rU   stock_antesrV   costo_unitariocantidad_entregadavalor_total_salidastock_despuesrY   rZ   r   rh   r8   r9   rY      s   


zMuestraComercial.savec                 C   r   )NzMuestra de r   r   r@   r8   r8   r9   rB      r   zMuestraComercial.__str__)r1   r2   r3   r   r{   r	   r|   rT   r   r   rJ   r]   r   r   r   r   r}   entregado_porr   r   r   r   r:   rY   rB   r   r8   r8   rh   r9   r      s    r   c                       s   e Zd ZejeejdZejeejdZ	eje
ejdZejejdZejdddZejdddZejdddddZejdddZejdddddZejdd	dddZG d
d dZ fddZ  ZS )RecepcionProduccionr   r   r   r   r   Tr   r   r+   r   r   r   c                   @   s   e Zd ZdZdZdZdgZdS )zRecepcionProduccion.Meta,mov_recepcion_produccion_recepcionproduccionu   Recepción de Producciónu   Recepciones de Producciónr   N)r1   r2   r3   r4   r$   r5   r6   r8   r8   r8   r9   r:     s
    
r:   c                    s   | j | j | _| jjptd}| j | }| j| j }|| | _| jr(| j| j ntd| _| j| j_	| j| j_| jj
ddgd t j
|i | d S )N0rU   rV   r   )r   cantidad_producidar   rT   rV   r   costo_unitario_produccionr   nuevo_costo_ponderadorU   rY   rZ   )rA   r[   r\   rc   total_existentetotal_nuevorh   r8   r9   rY     s   



zRecepcionProduccion.save)r1   r2   r3   r   r{   r	   r|   rT   r   plantar   maquinar   r   rJ   r]   r   r   r   r   r   r   r   r:   rY   r   r8   r8   rh   r9   r      s&    r   c                       s   e Zd ZejeejdZejdde	e
dgdZejdddZejejdZejeejdZG d	d
 d
Z fddZdd Z  ZS )MovimientoVentar   r   r   z0.0001r   r   r   r   c                   @   r   )zMovimientoVenta.MetaVentaVentasr   Nr   r8   r8   r8   r9   r:   ;  r   r:   c                    s8   | j  j| j8  _| j jdgd t j|i | d S r   rT   rU   rW   rY   rZ   r   rh   r8   r9   rY   @     zMovimientoVenta.savec                 C      d| j  d| jj d| j S )Nz	Venta de  de z a $)rW   rT   r   precio_unitario_ventar@   r8   r8   r9   rB   F  rC   zMovimientoVenta.__str__)r1   r2   r3   r   r{   r	   r|   rT   r   r   r   rW   r   r   r   rJ   r]   r}   
creado_porr:   rY   rB   r   r8   r8   rh   r9   r   1  s    r   c                       s   e Zd ZejeejdddZejeejdZ	ej
ej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
Zejddd	d	d
Z fddZdd Z  ZS )DevolucionCompradevoluciones_comprau   Ítem de orden origenr    r!   r$   r   r   r   r   r   Tr   r   r   c                    s   | j | j | _| j | jj }| j| j }|| }| jdkr"|| j nd| _|| _| j| j_| j| j_| jj	ddgd t
 j	|i | d S Nr   rU   rV   r   )r   cantidad_devueltar   rT   rV   precio_unitario_devueltonuevo_precio_ponderador   rU   rY   rZ   )rA   r[   r\   valor_inventariovalor_devolucionnuevo_valorrh   r8   r9   rY   Y  s   

zDevolucionCompra.savec                 C   r   )N   Devolución r   r   r@   r8   r8   r9   rB   e  r   zDevolucionCompra.__str__)r1   r2   r3   r   r{   r   r|   r`   r	   rT   r   r   rJ   r]   r   r   r   r   r   r   r   rY   rB   r   r8   r8   rh   r9   r   I  s"    r   c                       s   e Zd ZejeejdddZejeejdZ	ej
dddZejejdZejeejdZejd	d	d
Z fddZdd Z  ZS )DevolucionMuestraComercialdevoluciones_muestrazMuestra origenr   r   r   r   r   r   T)r+   r   c                    s8   | j  j| j7  _| j jdgd t j|i | d S r   r   r   rh   r8   r9   rY   v  s   zDevolucionMuestraComercial.savec                 C   r   Nu   Devolución de r   z el rW   rT   r   r]   r@   r8   r8   r9   rB   {  rC   z"DevolucionMuestraComercial.__str__)r1   r2   r3   r   r{   r   r|   muestrar	   rT   r   rW   r   r   rJ   r]   r}   rv   r   
comentariorY   rB   r   r8   r8   rh   r9   r   i  s    r   c                       s   e Zd ZejeejdddZejeejdZ	ej
dddZej
dddZejejdZejd	d
Zej
ddddZej
ddddZej
ddddZej
ddddZej
ddddZ fddZdd Z  ZS )DevolucionProducciondevoluciones_produccionu   Recepción origenr   r   r   r   r   r   Tr*   Fr   r   r   c                    s   | j j| _| j jp
d| _| j| j }|dkr.| j| j }| j| j }|| | _| j| | _nd| _| j| _|| _	|| j _| j| j _| j j
ddgd t j
|i | d S r   )rT   rU   r   rV   precio_costo_antesr   r   r   nuevo_precio_costor   rY   rZ   )rA   r[   r\   restanter   total_salidarh   r8   r9   rY     s   


zDevolucionProduccion.savec                 C   s   d| j  d| jj S )Nr   z unidades de )r   rT   r   r@   r8   r8   r9   rB     r   zDevolucionProduccion.__str__)r1   r2   r3   r   r{   r   r|   	recepcionr	   rT   r   r   r   r   r   rJ   r]   r   r   r   r   r   r   r   rY   rB   r   r8   r8   rh   r9   r     s&    r   c                       s   e Zd ZejeejdddZejeejdZ	ej
dddZej
dddZejejd	Zejd
dZG dd dZ fddZdd Z  ZS )MovimientoDevolucionVentadevoluciones_ventazVenta origenr   r   r   r   r   r   r   Tr*   c                   @   r   )zMovimientoDevolucionVenta.Metau   Devolución de VentazDevoluciones de Ventar   Nr   r8   r8   r8   r9   r:     r   r:   c                    s   | j s7| j j| j7  _| jjdgd ddlm} | jjdd}|| j|| j}t	d| d| j d	|  t
 j|i | d S )
NrU   r   r   )calcular_precio_venta_ponderadorF   )dayu#   💡 Nuevo precio venta ponderado (u   →z): )rI   rT   rU   rW   rY   
siip.utilsr   r]   replaceprintrZ   )rA   r[   r\   r   iniciopondrh   r8   r9   rY     s   zMovimientoDevolucionVenta.savec                 C   r   r   r   r@   r8   r8   r9   rB     rC   z!MovimientoDevolucionVenta.__str__)r1   r2   r3   r   r{   r   r|   ventar	   rT   r   rW   precio_venta_unitarior   r   rJ   r]   r   r   r:   rY   rB   r   r8   r8   rh   r9   r     s    
r   c                       sx   e Zd ZejeejddZejddddZ	ej
ejddZejd	d
ddZejd
ddZ fddZ fddZ  ZS )MovimientoPerdidaProcesoperdidas_en_procesor   
   r   u   Cantidad de unidades dañadas)r   r   	help_textu   Fecha y hora de la pérdida)r)   r      Tu,   Descripción breve del motivo de la pérdida)r'   r+   r   zComentarios adicionales)r+   r   c                    s`   | j rtjj| j d}| j|j }n| j}t j|i | | j j|8  _| jjdgd d S )NrH   rU   r   )	rI   r   rL   rQ   rW   rZ   rY   rT   rU   )rA   r[   r\   prevdeltarh   r8   r9   rY     s   zMovimientoPerdidaProceso.savec                    s8   | j  j| j7  _| j jdgd t j|i | d S r   )rT   rU   rW   rY   rZ   deleter   rh   r8   r9   r    r   zMovimientoPerdidaProceso.delete)r1   r2   r3   r   r{   r	   r|   rT   r   rW   r   r   rJ   r]   r   motivor   r   rY   r  r   r8   r8   rh   r9   r     s2    r   N)(	django.dbr   django.core.exceptionsr   django.core.validatorsr   django.utilsr   decimalr   proveedores.modelsr   r   referencias.modelsr	   listas_configuracion.modelsr
   r   django.contrib.authr   fabricacion.modelsr   r   django.db.models.signalsr   django.dispatchr   r}   Modelr   r   r   r   r   r   r   r   r   r   r   r8   r8   r8   r9   <module>   s0    mN 1 )