o
    %Ah3                      @   s   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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dS )    )models)get_user_model)timezone)Decimal)Employee
DepartmentNc                   @   s`   e Zd ZejdddZejddZejddZ	ej
ddZej
ddZdd	 ZG d
d dZdS )ExpenseCategoryd   T)
max_lengthuniqueblankdefaultauto_now_addauto_nowc                 C   s   | j S N)nameself r   2/var/www/html/optinet_system/expenditure/models.py__str__   s   zExpenseCategory.__str__c                   @   s   e Zd ZdgZdZdS )zExpenseCategory.Metar   zExpense CategoriesN)__name__
__module____qualname__orderingverbose_name_pluralr   r   r   r   Meta   s    r    N)r   r   r   r   	CharFieldr   	TextFielddescriptionBooleanField	is_activeDateTimeField
created_at
updated_atr   r    r   r   r   r   r      s    r   c                   @   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dZejddedd	Zejd
eddZe Ze ZejddZejddZejeejdddZejddZejddZedd Zedd Z dd Z!G dd dZ"dS )BudgetAllocation))monthlyMonthly)	quarterly	Quarterly)yearlyYearlybudget_allocations	on_deleterelated_nameallocations      
max_digitsdecimal_places0.00)r8   r9   r      r*   r
   choicesr   Tr   r   created_allocations)r2   nullr3   r   r   c                 C   s   | j | j S r   allocated_amountspent_amountr   r   r   r   remaining_amount-   s   z!BudgetAllocation.remaining_amountc                 C   s   | j dkr| j| j  d S dS )Nr   r	   r@   r   r   r   r   utilization_percentage1   s   
z'BudgetAllocation.utilization_percentagec                 C   s   | j j d| jj d| j S )N - z: KSh )employee	full_namecategoryr   rA   r   r   r   r   r   7   s   zBudgetAllocation.__str__c                   @   s   e Zd ZdgZg dZdS )zBudgetAllocation.Metaz-created_at)rF   rH   
start_dateend_dateN)r   r   r   r   unique_togetherr   r   r   r   r    :   s    r    N)#r   r   r   ALLOCATION_PERIOD_CHOICESr   
ForeignKeyr   CASCADErF   r   rH   DecimalFieldrA   r   rB   r!   allocation_period	DateFieldrI   rJ   r"   notesr$   r%   UserSET_NULL
created_byr&   r'   r(   propertyrC   rD   r   r    r   r   r   r   r)      s(    

r)   c                       s  e Zd Zg dZejddddZejeej	ddZ
ejeej	ddZejeejdddZejd	ejdddd
ZejdddZejddZejdddZejejdZejddddZejddddZejdeddZejddZejeejdddd
Z ejdddZ!ejeejdddd
Z"ejdddZ#ej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Z(ej)dd!d"Z*ejddZ+ejdd#Z, fd$d%Z-d&d' Z.G d(d) d)Z/  Z0S )*Expense))pendingzPending ApprovalapprovedApprovedrejectedRejected)paidPaid2   TF)r
   r   editableexpensesr1   )r2   r?   r   z
hr.Payslip)r2   r?   r   r3      z Brief description of the expenser
   	help_textz0Detailed description of what this expense is for)rf   r5   r6   r7   r   zName of vendor/supplier)r
   r   rf   r	   zVendor contact infor;   rX   r<   r   approved_expenses)r?   r   rejected_expensesr   )r
   r   z2Should this expense be reimbursed to the employee?)r   rf   r   c                    s   | j s4t d}tjjd| dd }|r)t	|j dd  }|d }nd}d| |d| _ t
 j|i | | jrh| jdv rjtjj| jd	d
gdjtddd p\td}|| j_| j  d S d S d S )Nz%Y%mEXP)expense_number__startswithz-expense_number   04d)rZ   r_   rZ   r_   )budget_allocation
status__inamount)totalrq   r:   )expense_numberr   nowstrftimerW   objectsfilterorder_byfirstintsupersavern   status	aggregater   Sumr   rB   )r   argskwargs
year_monthlast_expenselast_number
new_numbertotal_spent	__class__r   r   r{   m   s6   


zExpense.savec                 C   s   | j  d| j S NrE   )rr   titler   r   r   r   r      s   zExpense.__str__c                   @      e Zd ZdgZdS )zExpense.Metaz-submitted_atNr   r   r   r   r   r   r   r   r           
r    )1r   r   r   STATUS_CHOICESr   r!   rr   rM   r   rN   rF   r   rH   r)   rT   rn   related_payslipr   r"   r#   rO   rp   rQ   r   rs   expense_datevendor_namevendor_contactr|   r&   submitted_atrS   approved_byapproved_atrejected_byrejected_atrejection_reasonpaid_atpayment_methodpayment_referencerR   r$   is_reimbursabler'   r(   r{   r   r    __classcell__r   r   r   r   rW   >   s<    rW   c                       s   e Zd Zg dZejeejddZej	deddZ
ej	ddd	Zejd
dZejddZejdddZ fddZedd Zdd ZG dd dZ  ZS )ExpenseAttachment))receiptReceipt)invoiceInvoice)photoPhoto)documentDocument)otherOtherattachmentsr1   r;   r   r<   rd   zDescription of the attachmentre   zexpenses/attachments/)	upload_toTr   zFile size in bytesF)rf   rb   c                    s&   | j r| j j| _t j|i | d S r   )filesize	file_sizerz   r{   )r   r   r   r   r   r   r{      s   
zExpenseAttachment.savec                 C   sJ   | j r#| j }dD ]}|dk r|dd|   S |d }q|ddS dS )z)Return file size in human readable format)BKBMBGBi   z.1f z TBUnknown)r   )r   r   unitr   r   r   file_size_formatted   s   
z%ExpenseAttachment.file_size_formattedc                 C   s   | j j d| j S r   )expenserr   r   r   r   r   r   r      s   zExpenseAttachment.__str__c                   @   r   )zExpenseAttachment.Metaz-uploaded_atNr   r   r   r   r   r       r   r    )r   r   r   ATTACHMENT_TYPE_CHOICESr   rM   rW   rN   r   r!   attachment_typer   	FileFieldr   r&   uploaded_atPositiveIntegerFieldr   r{   rV   r   r   r    r   r   r   r   r   r      s    
r   c                   @   st   e Zd Zg dZejeejddZej	dedZ
ejeejddZejddZejdd	Zd
d ZG dd dZdS )ExpenseApprovalLog))	submitted	SubmittedrY   r\   )r_   zMarked as Paid)modifiedModifiedapproval_logsr1   r;   )r
   r=   T)r2   r?   r   r   c                 C   s   | j j d|   S r   )r   rr   get_action_displayr   r   r   r   r      s   zExpenseApprovalLog.__str__c                   @   r   )zExpenseApprovalLog.Metaz
-timestampNr   r   r   r   r   r       r   r    N)r   r   r   ACTION_CHOICESr   rM   rW   rN   r   r!   actionrS   rT   performed_byr"   commentsr&   	timestampr   r    r   r   r   r   r      s    r   )	django.dbr   django.contrib.authr   django.utilsr   decimalr   	hr.modelsr   r   uuidrS   Modelr   r)   rW   r   r   r   r   r   r   <module>   s   %Q'