B
    b'                 @   s   d dl Z d dl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 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ZG dd deZeeG dd deZdS )    N)	ModelForm)get_user_model)settings)gettext_lazy)admin)forms)ZKModelAdmin)db_const)Company)AdminRuntimeWarning)GeneralActionDelete)STATUS_VALIDc               @   s   e Zd ZejedejdZejdedej	dZ
ejdedejdZejdedej	dZejdedejdZG d	d
 d
ZdS )CompanyFormZcompany_field_name)label
max_lengthFZcompany_field_company_phone)requiredr   r   Zcompany_field_contact_nameZcompany_field_contact_phoneZcompany_field_company_addressc               @   s   e Zd ZeZdZdS )zCompanyForm.Meta)namecompany_phonecontact_namecontact_phonecompany_addressN)__name__
__module____qualname__r
   ZmodelsZfields r   r   =G:\easytimepro\master/mysite/personnel\admin\company_admin.pyMeta   s   r   N)r   r   r   r   Z	CharField_r	   ZMAX_COMPANY_NAMEr   ZMAX_COMPANY_PHONEr   ZMAX_OWNER_NAMEr   r   ZMAX_COMPANY_ADDRESSr   r   r   r   r   r   r      s   r   c                   s   e Zd Z fddZ  ZS )CompanyDeletec                s  ddl m} ddlm} ddlm} i }g }x0| jD ]$}|jpF|j}t	|| j
jjksht	|tjkr~|| td||< |jj|dd }|jj|d }	|s|	r|| td|j||< q6|jj|d	j| j
jjd
}
xr|
D ]j}|jj|dj|d}| sDtjj|d
}|| td|j|j||< q| j|_|  qW q6W |jj| jdj|d}|  tt| j|d dS )z
        check:
        the company is current user's company;
        check exists stuff or terminal or not;
        some user only in the company or not;
        r   )
Membership)Terminal)MyUserz2Current company & default company can't be delete.)
company_idrole)r"   z&Company {} still have staff or device.)current_company)id)userz"user {} only exists in company {}.)Zcompany_id__in)remove_deniedN) mysite.base.modelsr   Zmysite.iclock.modelsr    mysite.accounts.modelsr!   objectsr%   Zpkstrrequestr&   r$   r   ZDEFAULT_COMPANY_IDappendr   filterexistsformatr   Zexcluder
   getusernamefirstr"   savedeletesuperr   action)selfr   r    r!   r'   Zundelete_objobjZobj_idZuser_existsZterminal_existsZuser_in_current_companyr&   
membershipZcompany_obj)	__class__r   r   r7   #   s:     




zCompanyDelete.action)r   r   r   r7   __classcell__r   r   )r;   r   r   "   s   r   c               @   s   e Zd ZdZdZdZeZegZ	dd Z
dd Zede_d	d
 Zede_dd Zede_dd Zedd Zedd Zedd Zdd Zdd Zdd Zd"ddZed d! ZdS )#CompanyAdminz9
    this admin display just for enterprise version.
    )r   )
r%   r   r   r   r   r   device_countemployee_count
user_countZhex_idc             C   sN   ddl }|jj}|sg S | jdkr0| jdkr0g S | |}dd ||D S )z7
        only support superuser delete company
        r   Nc             S   s   g | ]\}}}|||d qS ))r   multiactionsr   ).0r   rA   rB   r   r   r   
<listcomp>e   s    z0CompanyAdmin.get_action_sets.<locals>.<listcomp>)sixr&   is_superuserrB   action_setsZget_action_item_and_setZ
itervalues)r8   r,   rE   Zis_surperuserrG   r   r   r   get_action_setsY   s    
zCompanyAdmin.get_action_setsc             C   s
   |j  S )N)Zterminal_setcount)r8   r9   r   r   r   r>   h   s    zCompanyAdmin.device_countZarea_field_deviceCountc             C   s   |j jtd S )N)status)Zemployee_setr.   r   rI   )r8   r9   r   r   r   r?   m   s    zCompanyAdmin.employee_countZdepartment_filed_employeeCountc             C   s$   ddl m} |jj|jd }|S )Nr   )r   )r"   )r(   r   r*   r.   r%   rI   )r8   r9   r   r@   r   r   r   r@   r   s    zCompanyAdmin.user_countZcompany_field_uerCountc             C   sJ   ddl m} |j}|jr$tj }n"|jj|dd}tjj|d}|S )zJ
        superuser see all company;
        user see own company.
        r   )r   )r&   company)Zid__in)	r(   r   r&   rF   r
   r*   allr.   values)r8   r,   r   r&   ZquerysetZown_membershipsr   r   r   get_querysety   s    zCompanyAdmin.get_querysetc             C   s2   | j }td}tjj|d}|r.ttddS )z)
        check name exist or not
        z^[A-Za-z0-9-]+$)Zname__iexactzduplicate company nameN)r   recompiler
   r*   r.   r   r   )r9   company_nameZcompany_name_reZcompany_compare_objr   r   r   _check_company_rule   s
    
z CompanyAdmin._check_company_rulec       
      C   s   t j}t j}t j}t j}t j}t j}d}| j}| j}	t	||	d
||dddd| j| j| jtj tj tj|d |||||d} |   | S )	zU
        add the required field for fit cloud version's company model design
        Z	localhostz{}.{}ZDefaultINZ	Bangalore   )Zdays)r   r   Z
sub_domainZ	address_1ZcountryZcityZpayment_typer   r   r   Zsubscription_start_dateZsubscription_end_dateZmax_employees_limitZmax_devices_limitZmax_transactions_limitZmax_mobile_applications_limitZmax_whatsapp_push_limit)r   ZDEFAULT_SUBSCRIPTION_DAYSZDEFAULT_MAX_DEVICES_LIMITZDEFAULT_MAX_EMPLOYEES_LIMITZDEFAULT_MAX_TRANSACTIONSZ%DEFAULT_MAX_MOBILE_APPLICATIONS_LIMITZDEFAULT_MAX_WHATSAPP_PUSH_LIMITr   r   r
   r0   r   r   r   datetimeZnowZ	timedeltar4   )
r9   Zdefault_subscription_daysZdefault_max_devices_limitZdefault_max_employee_limitZdefault_max_transactions_limitZ%default_max_mobile_applications_limitZdefault_max_whatsapp_push_limitZhostrQ   r   r   r   r   _update_company_obj   s:    
z CompanyAdmin._update_company_objc             C   sV   ddl m} t }|j }x4|D ],}|jr"|jj| |dd }d|_|  q"W dS )zL
        create membership for assign this company to all superuser
        r   )r   )rK   r&      N)	r(   r   r   r*   rL   rF   Zget_or_creater#   r4   )r9   r   Z
class_userZusersr&   r:   r   r   r   _assign_membership   s    

zCompanyAdmin._assign_membershipc             C   s   |  |}| | dS )z:
        check and save for add company operation
        N)rV   rX   )r8   r9   r   r   r   _add_company_operation   s    
z#CompanyAdmin._add_company_operationc             C   s   |s|  | n|  dS )zA
        Given a model instance save it to the database.
        N)rY   r4   )r8   r,   r9   formZchanger   r   r   
save_model   s    zCompanyAdmin.save_modelc             C   s&   t |jdr|jj}nd}|s"dS d S )NrF   F)hasattrr&   rF   )r8   r,   rF   r   r   r   has_add_permission   s
    
zCompanyAdmin.has_add_permissionNc             C   s&   t |jdr|jj}nd}|s"dS d S )NrF   F)r\   r&   rF   )r8   r,   r9   rF   r   r   r   has_delete_permission   s
    
z"CompanyAdmin.has_delete_permissionc              C   s8   ddl m}  | jjdd }|r4| jjdd  d S )Nr   )r!   ZAnonymousUser)r2   )r)   r!   r*   r.   r/   r1   r5   )r!   Zhas_anonymous_userr   r   r   initial_data   s    zCompanyAdmin.initial_data)N)r   r   r   __doc__Zlist_filterZlist_displayr   rZ   r   rB   rH   r>   r   Zshort_descriptionr?   r@   rN   staticmethodrR   rV   rX   rY   r[   r]   r^   r_   r   r   r   r   r=   N   s*   


$	
r=   )rO   rU   Zdjango.formsr   Zdjango.contrib.authr   Zdjango.confr   Zdjango.utils.translationr   r   mysiter   Zmysite.adminr   Zmysite.admin.kernelr   Zmysite.personnelr	   Zmysite.cloud.modelsr
   Zmysite.admin.exceptionsr   Zmysite.admin.actionr   Zmysite.admin.modelsr   r   r   registerr=   r   r   r   r   <module>   s"   ,