
    vg(                     l   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZmZ ddlmZ ddlmZ e ed      d               Zee ed      d                      Zee ed      d                      Zee ed      d                      Z ee ed      d                      Z!ee ed      d                      Z"ee ed      d                      Z#ee ed      d                      Z$ee ed      d                      Z%e ed      d               Z&y)a  
tax_views.py

This module contains view functions for handling federal tax-related operations.

The functions in this module handle various tasks related to payroll, including creating
filing status, managing tax brackets, calculating federal tax, and more. These functions
utilize the Django framework and make use of the render and redirect functions from the
django.shortcuts module.

    N)parse_qs)messages)HttpResponseHttpResponseRedirectJsonResponse)redirectrender)gettext_lazy)get_key_instances)hx_request_requiredlogin_requiredpermission_required)FilingStatusFormTaxBracketForm)FilingStatus)
TaxBracketzpayroll.view_filingstatusc                 d    t         j                  j                         }d}d|i}t        | ||      S )z
    Display the filing status view.

    This view retrieves all filing statuses from the database and renders the
    'payroll/tax/filing_status_view.html' template with the filing status data.

    z#payroll/tax/filing_status_view.htmlstatus)r   objectsallr	   )requestr   templatecontexts       +/var/www/horilla/payroll/views/tax_views.pyfiling_status_viewr      s6     !!%%'F4H G'8W--    zpayroll.add_filingstatusc                 x   t               }| j                  dk(  rt        | j                        }|j                         rn|j	                          t        j                  | t        d             t               }t        t        j                  j                               dk(  rt        d      S t        | dd|i      S )z
    Create a filing status record for tax bracket based on user input.

    If the request method is POST and the form data is valid, save the filing status form
    and redirect to the create-filing-status page.

    POSTz#Filing status created successfully    )<script>window.location.reload()</script>z'payroll/tax/filing_status_creation.htmlform)r   methodr   is_validsaver   success_lenr   r   filterr   r	   )r   filing_status_forms     r   create_filing_statusr*   ,   s     *+~~-gll;&&(##%Wa(M&NO!1!3<''..01Q6#$OPP1&	
 r   zpayroll.change_filingstatusc                    t        j                  |      }|sDt        j                  | t	        d             t        | j                  j                  dd            S t        |      }| j                  dk(  rVt        | j                  |      }|j                         r/|j                          t        j                  | t	        d             t        | dd|i      S )	a  
    Update an existing filing status record based on user input.

    If the request method is POST and the form data is valid, update the filing status form
    and redirect to the update-filing-status page.

    :param tax_bracket_id: The ID of the filing status to update.
    zFiling status not foundHTTP_REFERER/instancer   z#Filing status updated successfully.z#payroll/tax/filing_status_edit.htmlr!   )r   findr   errorr&   r   METAgetr   r"   r   r#   r$   r%   r	   )r   filing_status_idfiling_statusr)   s       r   update_filing_statusr6   I   s     !%%&67Mw"; <=#GLL$4$4^S$IJJ)=A~~-gll]S&&(##%Wa(M&NO-&	
 r   zpayroll.delete_filingstatusc                 >   t        j                  |      }|r0|j                          t        j                  | t        d             nt        j                  | t        d             t         j                  j                         st        d      S t        t              S )z
    Delete a filing status.

    This view deletes a filing status with the given `filing_status_id` from the
    database and redirects to the filing status view.

    z#Filing status successfully deleted.z!This filing status was not found.r    )r   r0   deleter   infor&   r1   r   existsr   r   filing_status_search)r   r4   r5   s      r   filing_status_deleter<   h   sv     !%%&67Mgq!FGHw"E FG&&(GHH())r   c                 B   | j                   j                  d      r| j                   j                  d      nd}t        j                  j	                  |      }| j                   j                         }t        |      }t        t        |       |||d}t        | d|      S )aT  
    Display the filing status search view.

    This view handles the search functionality for filing statuses. It retrieves
    the search term from the GET parameters, filters the FilingStatus objects
    based on the search term, and renders the 'payroll/tax/filing_status_list.html'
    template with the filtered filing statuses.
    search )filing_status__icontains)r   pdfilter_dictz#payroll/tax/filing_status_list.html)	GETr3   r   r   r(   	urlencoder   r   r	   )r   r>   r   previous_data	data_dictr   s         r   r;   r;   ~   s     +2++//(*CW[[__X&F!!((&(IFKK))+M'IlI. G
 '@'JJr   zpayroll.view_taxbracketc                     t         j                  j                  |      }t        j                  j	                  |      j                  d      }||d}t        | d|      S )a  
    Display a list of tax brackets for a specific filing status.

    This view retrieves all tax brackets associated with the given `filing_status_id`
    and renders them in the "tax_bracket_view.html" template.

    Args:
        request: The HTTP request object.
        filing_status_id: The ID of the filing status for which to display tax brackets.

    Returns:
        The rendered "tax_bracket_view.html" template with the tax brackets for the
        specified filing status.
    )idr4   
max_income)tax_bracketsr5   z!payroll/tax/tax_bracket_view.html)r   r   r3   r   r(   order_byr	   )r   r4   r5   rK   r   s        r   tax_bracket_listrM      se    $ !((,,0@,AM%%,,) - h|   ,mLG'>HHr   zpayroll.add_taxbracketc                    t        d|i      }||d}| j                  dk(  rt        | j                  d|i      }|j                         r|j                  j                  d      }|s>t        j                  | t        d             t        j                  |j                  _        |j                          t        j                  | t        d             t        t         |      S ||d	<   t#        | d
|      S )z
    Create a tax bracket record for federal tax calculation based on user input.

    If the request method is POST and the form data is valid, save the tax bracket form
    and redirect to the tax-bracket-create page.

    r4   )initialr!   r4   r   rJ   #The maximum income will be infinitez)The tax bracket was created successfully.rI   r!   z%payroll/tax/tax_bracket_creation.html)r   r"   r   r#   cleaned_datar3   r   r9   r&   mathinfr/   rJ   r$   r%   r   create_tax_bracketr	   )r   r4   tax_bracket_formr   rJ   s        r   rU   rU      s     &/ACS.TU ,G ~~)LL#57G"H
 $$&)66::<HJgq)N'OP7;xx ))4!!#Wa(S&TU.AQRR*'BGLLr   zpayroll.change_taxbracketc                    t        j                  |      }|r|j                  j                  }t	        |      }| j
                  dk(  rt	        | j                  |      }|j                         r|j                  j                  d      }|s>t        j                  | t        d             t        j                  |j                  _        |j#                          t        j$                  | t        d             ||d}t'        | d|      S t        j(                  | t        d             t+        | j,                  j                  d	d
            S )a  
    Update an existing tax bracket record based on user input.

    If the request method is POST and the form data is valid, update the tax bracket form
    and redirect to the tax-bracket-create page.

    :param tax_bracket_id: The ID of the tax bracket to update.
    r.   r   rJ   rQ   z.The tax bracket has been updated successfully.rP   z!payroll/tax/tax_bracket_edit.htmlTax bracket not foundr,   r-   )r   r0   r4   rH   r   r"   r   r#   rR   r3   r   r9   r&   rS   rT   r/   rJ   r$   r%   r	   r1   r   r2   )r   tax_bracket_idtax_bracketr4   rV   rJ   r   s          r   update_tax_bracketr[      s    //.1K&77::);?>>V#-gll[Q((*-::>>|L
!MM'1-R+ST;?88$--8 %%'  QOP
 % 0
 gBGLLNN7A567 0 0 EFFr   zpayroll.delete_taxbracketc                 r   t        j                  |      }|r&|j                         r|j                  j                  nd}|r t        j                  | t        d             nt        j                  | t        d             |rt        t        |      S t        | j                  j                  dd            S )z
    Delete an existing tax bracket record.

    Retrieve the tax bracket with the specified ID and delete it from the database.
    Then, redirect to the tax-bracket-create page.

    :param tax_bracket_id: The ID of the tax bracket to delete.
    Nz!Tax bracket successfully deleted.rX   rI   r,   r-   )r   r0   r8   r4   rH   r   r%   r&   r1   r   rM   r   r2   r3   )r   rY   rZ   r4   s       r   delete_tax_bracketr]      s     //.1K ;--/ 	$$'' 
 !$G"HIw"9 :;  	!4DE "',,"2"2>3"GHr   c                     | j                   d   }t        j                  j                  |      }|j                  |k(  s||_        |j                          t        ddi      S )z<
    Ajax method to update python code of filing status
    code)pkmessager%   )r   r   r   r3   python_coder$   r   )r   r`   r_   filings       r   update_py_coderd     sZ     <<D!!%%%,F%!I.//r   )'__doc__rS   urllib.parser   django.contribr   django.httpr   r   r   django.shortcutsr   r	   django.utils.translationr
   r&   base.methodsr   horilla.decoratorsr   r   r   payroll.forms.tax_formsr   r   payroll.models.modelsr   payroll.models.tax_modelsr   r   r*   r6   r<   r;   rM   rU   r[   r]   rd    r   r   <module>rq      s  
  ! # H H - 6 * W W D . 0 01. 2 . /0 1  4 23 4  8 23* 4  *& 01K 2  K, ./I 0  I. -.M /  M> 01G 2  GD 01 2  4 01	0 2 	0r   