
    n^j0                     F   d Z ddl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 dd
lmZmZ ddlmZmZmZmZmZ ddlmZmZmZm Z  d Z!d Z"e ed      d               Z#e ed      d               Z$e ed      d-d              Z%e ed       eddg      d                      Z&e ed      d               Z'e ed      d-d              Z(e ed       eddg      d                      Z)e ed      d                Z*e ed!      d-d"              Z+e ed#       eddg      d$                      Z,e ed%      d&               Z-e ed'      d-d(              Z.e ed)       eddg      d*                      Z/e ed%      d+               Z0d, Z1y).u'  
revenue/views.py
================
Function-based views for Revenue Management (Module 4).
Includes CRUD endpoints, dashboard, and report with Excel export.

NOTE: ``horilla.decorators`` (NOT ``horilla_views.cbv_methods``) must be
used here — the latter expects a CBV ``self`` first-argument.
    N)date)messages)QSumHttpResponse)get_object_or_404render)gettext_lazy)require_http_methods)login_requiredpermission_required   )RevenueHeadFormRevenueItemFormRevenueRecordFormRevenueReportFilterFormRevenueSubHeadForm)RevenueHeadRevenueItemRevenueRecordRevenueSubHeadc                      t        d      S )zCStandard HTMX response to close the open modal and reload the page.zw<script>$('#genericModal').removeClass('oh-modal--show');$('#reloadMessagesButton').click();location.reload();</script>r        1/var/www/python-projects/worksol/revenue/views.py_close_modal_reloadr   &   s    	 r   c                      t        d      S )z;Standard response after a delete triggered from a list row.zO<script>$('.filterButton').click();$('#reloadMessagesButton').click();</script>r   r   r   r   _hx_delete_responser   1   s    	 r   zrevenue.view_revenueheadc           	      >   t        j                         }|j                  }|j                  }t        j
                  j                  |      }|j                  |      }|j                  t        d            d   xs d}|j                  t        d            d   xs d}t        j
                  j                  |      j                  ddd	      j                  t        d      
      j                  d      }t        j
                  j                  ddd      j                  dd      d d }	||||	|t        j
                  j                  d      j                         t        j
                  j                  d      j                         d}
t!        | d|
      S )N)transaction_date__year)transaction_date__monthamounttr%   r   item__sub_head__head__iditem__sub_head__head__codeitem__sub_head__head__name)totalz-totalitem__sub_head__head
departmentrecorded_by-transaction_date-created_at
   T	is_active)	ytd_total	mtd_totalby_headrecentcurrent_year
head_count
item_countzrevenue/dashboard.html)r   todayyearmonthr   objectsfilter	aggregater   valuesannotateorder_byselect_relatedr   countr   r
   )requestr9   r6   current_monthytd_recordsmtd_recordsr2   r3   head_totalsr5   contexts              r   revenue_dashboardrJ   @   sy    JJLE::LKKM''..l.SK$$]$KK%%H%6s;@qI%%H%6s;@qI 	$$L$I	*,HJf	g	H	&	(		  	,,-C\S`a	%}	5cr	;  $!))0040@FFH!))0040@FFHG '3W==r   c                     t         j                  j                  d      j                  d      j	                  d      }t        | dd|i      S )NTr0   	sub_headsnamezrevenue/head_list.htmlheads)r   r<   r=   prefetch_relatedrA   r
   )rD   rN   s     r   revenue_head_listrP   k   sL     	""T"2		+	&	&	 

 '3gu5EFFr   zrevenue.add_revenueheadc                 J   |rt        t        |      nd }t        | j                  xs d |      }| j                  dk(  rI|j                         r9|j                          t        j                  | t        d             t               S t        | d|t        d      |d u d      S )NpkinstancePOSTzRevenue head saved. revenue/partials/form_modal.htmlRevenue Headformtitle	is_create)r	   r   r   rV   methodis_validsaver   success_r   r
   rD   rS   rU   rZ   s       r   revenue_head_formrc   v        9; 4H7<</4(CD~~DMMO		!$9":;"$$*. 1D@PQ r   zrevenue.delete_revenueheadrV   DELETEc                     t        t        |      }d|_        |j                  dg       t	        j
                  | t        d             t               S )NrR   Fr1   update_fieldszRevenue head deactivated.)r	   r   r1   r_   r   r`   ra   r   rD   rS   objs      r   revenue_head_deleterk      F     KB
/CCMHHK=H)Wa ;<=  r   zrevenue.view_revenuesubheadc                     | j                   j                  d      }t        j                  j	                  d      j                  d      j                  dd      }|r|j	                  |      }t        | dd|i      S )	NheadTr0   
head__namerM   )head_idzrevenue/sub_head_list.htmlrL   )GETgetr   r<   r=   rB   rA   r
   )rD   rp   qss      r   revenue_sub_head_listrt      st     kkoof%G				&	&	&	6	E	Ef	M	V	Vf
B YYwY''7+r9JKKr   zrevenue.add_revenuesubheadc                    |rt        t        |      nd }i }|s| j                  j                  d      |d<   t	        | j
                  xs d ||      }| j                  dk(  rI|j                         r9|j                          t        j                  | t        d             t               S t        | d|t        d      |d u d      S )	NrR   rn   )rU   initialrV   zRevenue sub-head saved.rW   zRevenue Sub-HeadrY   )r	   r   rq   rr   r   rV   r]   r^   r_   r   r`   ra   r   r
   )rD   rS   rU   rv   rZ   s        r   revenue_sub_head_formrw      s     <> B74HG!++//&1gll2dXwWD~~DMMO		!$=">?"$$*"4 5HPTDTU r   zrevenue.delete_revenuesubheadc                     t        t        |      }d|_        |j                  dg       t	        j
                  | t        d             t               S )NrR   Fr1   rg   zRevenue sub-head deactivated.)r	   r   r1   r_   r   r`   ra   r   ri   s      r   revenue_sub_head_deletery      sF     Nr
2CCMHHK=H)Wa ?@A  r   zrevenue.view_revenueitemc                     | j                   j                  d      }t        j                  j	                  d      j                  d      j                  ddd      }|r|j	                  |      }t        | d	d
|i      S )Nsub_headTr0   sub_head__headsub_head__head__namesub_head__namerM   )sub_head_idzrevenue/item_list.htmlitems)rq   rr   r   r<   r=   rB   rA   r
   )rD   sub_idrs   s      r   revenue_item_listr      sw     [[__Z(F				#	#d	#	3	B	BCS	T	]	] 0&
B YY6Y*'3gr]CCr   zrevenue.add_revenueitemc                 J   |rt        t        |      nd }t        | j                  xs d |      }| j                  dk(  rI|j                         r9|j                          t        j                  | t        d             t               S t        | d|t        d      |d u d      S )NrR   rT   rV   zRevenue item saved.rW   zRevenue ItemrY   )r	   r   r   rV   r]   r^   r_   r   r`   ra   r   r
   rb   s       r   revenue_item_formr      rd   r   zrevenue.delete_revenueitemc                     t        t        |      }d|_        |j                  dg       t	        j
                  | t        d             t               S )NrR   Fr1   rg   zRevenue item deactivated.)r	   r   r1   r_   r   r`   ra   r   ri   s      r   revenue_item_deleter      rl   r   zrevenue.view_revenuerecordc                     t         j                  j                  ddd      j                  dd      }| j                  j                  d      }|r|j                  |      }t        | dd	|i      S )
Nr*   r+   r,   r-   r.   item)item_idzrevenue/record_list.htmlrecords)r   r<   rB   rA   rq   rr   r=   r
   )rD   rs   r   s      r   revenue_record_listr      sl     
			-	-m
h"M2  kkoof%GYYwY''5	2GGr   zrevenue.add_revenuerecordc                    |rt        t        |      nd }t        | j                  xs d || j                        }| j
                  dk(  r{|j                         rk|j                  d      }| j                  |_        |j                          t        j                  | t        d      d|j                  iz         t               S t        | d|t        d	      |d u d
      S )NrR   )rU   request_userrV   F)commitzRevenue record %(ref)s saved.refrW   zRevenue RecordrY   )r	   r   r   rV   userr]   r^   r_   r,   r   r`   ra   reference_numberr   r
   )rD   rS   rU   rZ   records        r   revenue_record_formr      s     ;= 26$HW\\1THSZS_S_`D~~DMMO%($\\!$C"DvOfOfGg"gh"$$*"2 3(dBRS r   zrevenue.delete_revenuerecordc                     t        t        |      }|j                  }|j                          t	        j
                  | t        d      d|iz         t               S )NrR   zRevenue record %(ref)s deleted.r   )r	   r   r   deleter   r`   ra   r   )rD   rS   rj   r   s       r   revenue_record_deleter     sL     Mb
1C


CJJLWa ABeS\QR  r   c                    t        | j                  xs d       }t        j                  j	                  ddd      j                  d      }|j                         r|j                  }|j                  d      r|j                  |d         }|j                  d      r|j                  |d         }|j                  d      r|j                  |d   	      }|j                  d
      r|j                  |d
         }|j                  t        d            d   xs d}| j                  j                  d      dk(  rt        |      S ||||j                         d}t        | d|      S )Nr*   r+   r,   r-   	date_from)transaction_date__gtedate_to)transaction_date__lte)r+   revenue_head)r*   r#   r$   r%   r   exportexcel)rZ   r   r)   rC   zrevenue/report.html)r   rq   r   r<   rB   rA   r^   cleaned_datarr   r=   r>   r   _export_revenue_excelrC   r
   )rD   rZ   rs   dr)   rI   s         r   revenue_reportr     sF    #7;;#6$7D				-	-m
h"#  }}55;@B559>B55ao6B55 .0ABBLL3x=L)#.3!E {{x G+$R(( 	G '0'::r   c                    dd l }ddlm}m} |j	                         }|j
                  }d|_        g d} |dd      } |dd	
      }t        |d      D ]'  \  }	}
|j                  d|	|
      }||_	        ||_
        ) t        | d      D ]  \  }}|j                  |j                  |j                  j                  d      |j                  j                   |j                  j"                  j$                  j&                  |j                  j"                  j&                  |j                  j&                  |j(                  rt+        |j(                        ndt-        |j.                        |j0                  |j2                  r|j2                  j5                         ndg
        |j6                  D ]D  }	t9        d |	D        d      }t;        |dz   d      |j<                  |	d   j>                     _         F tC        jD                         }|jG                  |       |jI                  d       tK        |jM                         d      }d|d<   |S )Nr   )FontPatternFillzRevenue Report)
zReference #zTransaction DatezRevenue CoderX   zSub-HeadItem
Departmentu   Amount (₦)DescriptionzRecorded ByTFFFFFF)boldcolorsolid2C7BE5)fgColorr   )rowcolumnvalue   z%Y-%m-%d c              3   `   K   | ]&  }t        t        |j                  xs d              ( yw)r   N)lenstrr   ).0cells     r   	<genexpr>z(_export_revenue_excel.<locals>.<genexpr>e  s#     Bds3tzz/R01Bs   ,.r/   )default   (   zAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet)content_typez*attachment; filename="revenue_report.xlsx"zContent-Disposition)'openpyxlopenpyxl.stylesr   r   Workbookactiver[   	enumerater   fontfillappendr   transaction_datestrftimer   coder{   rn   rM   r+   r   floatr#   descriptionr,   get_full_namecolumnsmaxmincolumn_dimensionscolumn_letterwidthioBytesIOr_   seekr   read)rs   r   r   r   wbwsheadersheader_fontheader_fillcolhr   row_idxrecmax_lenbufresponses                    r   r   r   A  s   1				B	BBHG D1Kgx8KGQ'  Qww1Sw2		 
 ""a( 
		    ))*5HHMMHH""''HH""HHMM#&>>Cr#**OO/2COO))+B
 	 zz PBcBBO;>w{B;OSV1128P **,CGGCLHHQK
XH 'SH"#Or   )N)2__doc__r   datetimer   django.contribr   django.db.modelsr   r   django.httpr   django.shortcutsr	   r
   django.utils.translationr   ra   django.views.decorators.httpr   horilla.decoratorsr   r   formsr   r   r   r   r   modelsr   r   r   r   r   r   rJ   rP   rc   rk   rt   rw   ry   r   r   r   r   r   r   r   r   r   r   r   <module>r      sw   
  # # $ 6 6 = B  L K /0!> 1 !>R /0G 1 G ./ 0  12vx()! * 3 ! 23L 4 L 12 3 " 45vx()! * 6 ! /0D 1 D ./ 0  12vx()! * 3 ! 12H 3 H 01 2   34vx()! * 5 ! 12; 3 ;@0r   