
    J)i8                        d dl mZmZ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mZmZ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 d dlmZ d dl m!Z!  e       Z"e"jG                  de!       ee       ee      fde$dedefd       Z%e"jG                  d       ee       ee      fde$dedefd       Z&e"jG                  d       ee       ee      fde$dedefd       Z'y)    )	APIRouterDependsHTTPException)StreamingResponse)Session)BytesIO)A4)SimpleDocTemplate	ParagraphSpacerTable
TableStyleImage	PageBreak)getSampleStyleSheetParagraphStyle)colors)get_db)JWTPayloadSchemaget_current_student)Module4ReportService)Module4ReportPreviewz/preview4/{group_id})response_modelgroup_iddbtokenc                    K   	 t        ||      }|j                  |        d{   S 7 # t        $ r}t        dt	        |            d}~ww xY ww)zPreview Module 4 report dataN  status_codedetail)r   get_report_preview	Exceptionr   str)r   r   r   servicees        E/var/www/html/retail-simulation-api/app/api/module_4_report/router.pyget_module4_report_previewr(      sN     <&r51//9999 <CF;;<s0   A + )+ A+ 	AA

AAz/download4/{group_id}c                   K   	 t        ||      }|j                  |        d {   }t               }t        |t        dddd      }t               }g }	 t        ddd      }	d|	_        |j                  |	       |j                  t        dd	             t        d
|d   dddd      }
|j                  t        d|
             |j                  t        dd	             t        d|d   dd	d      }|j                  t        d|             |j                  t        d|d                |j                  t        dd             t        d|d   dd      }d|j                  j                  xs dgdt        |j                  j                  xs d      gd|j                  j                   xs dgd |j                  j"                  xs dgg}|D ]t  \  }}t%        t        ||      t        ||      ggd!d"gd#g$      }|j'                  t)        d%d&d'dt*        j,                  fd(d)d*d+d,d-d.d/g	             |j                  |       v |j                  t        dd0             |j                  t        d1|d                |j                  t        dd             d2|j                  j.                  xs dgd3|j                  j0                  xs dgg}|D ]t  \  }}t%        t        ||      t        ||      ggd!d"gd#g$      }|j'                  t)        d%d&d'dt*        j,                  fd(d)d*d+d,d-d.d/g	             |j                  |       v |j                  t        dd0             |j                  t        d4|             g d5g}|j2                  D ]
  }|j4                  s|j6                  s|j6                  j9                         d6k(  r$t        |j:                  j<                  xs d      }n|j6                  j9                         d7k(  r$t        |j:                  j>                  xs d      }nC|j6                  j9                         d8k(  r$t        |j:                  j@                  xs d      }nd9}|j                  |j4                  |j6                  |g        tC        |      dk(  r|j                  g d:       t%        |g d;d<gtC        |      z  $      }|j'                  t)        d%d&d'dt*        j,                  fd=d>d)d*d+d?d@g             |j                  |       |j                  t        dd             |j                  tE                      |j                  t        dA|             g dBg}|jF                  rtI        dC |jF                  D              rtK        |jF                        D ]  \  }}|jL                  s|jN                  xs d}|jP                  xs d}|d<k\  r|d<k\  rdD}n"|dEk\  rdF}n|dGk\  rdH}n|dIk\  rdJ}n
|d	k\  rdK}ndL}|j                  dM|dz    ||dNdO|dNdOg        n#tS        dP      D ]  }|j                  g dQ        t%        |g dRd<gtC        |      z  $      }|j'                  t)        d%d&d'dt*        j,                  fd=d>dSd*d+d,d-g             |j                  |       |j                  t        dd             |j                  t        dT|             g dU}|g}d} d}!d}"dV}#tK        |jF                        D ]  \  }}|jL                  sdW}#|jN                  xs d}|jP                  xs d}|r
|r||z  dXz  nd}$| |z  } |!|z  }!|"|$z  }"|j                  t        dM|dz    |      t        |dNdO|      t        |dNdO|      t        |$dYdO|      g        |#s#tS        dP      D ]  }|j                  g dZ        t%        |g dRdEgtC        |      z  $      }%|%j'                  t)        d%d&d'dt*        j,                  fd=d>dSd*d+d,d-d.d/g
             |j                  |%       |j                  t        dd	             t        d[|d   d\d	d]^      }&t        d_|&      |#r| dNdOnd`|#r|!dNdOnd`|#r|"dYdOnd`gg}'t%        |'g dRdEg$      }(|(j'                  t)        d%d&d'dt*        j,                  fdadSd*d+d,d-g             |j                  |(       |jU                  |       |jW                  d       tY        t        |j[                               dbdcdd|  deif      S 7 	##  Y xY w# t\        $ r})t_        dgt        |)      h      d })~)ww xY ww)iN2   )pagesize
leftMarginrightMargin	topMarginbottomMarginzTS Logo.png   K   )widthheightLEFT   
   CustomTitleHeading1      Helvetica-Bold)parentfontSize
spaceAfter	alignmentfontNamezModule 4: Category ManagementSectionHeaderHeading2   )r<   r=   r>   r@   zPrimary ResearchzCompetitor Research:NormalCentered	   )r<   r?   r=   zReference CompetitorN/AzNumber of Categories Stockedr   zKey Categories StockedzLWhat are some key observations around stocking and categories in the store? i,     (   )	colWidths
rowHeightsGRIDr   r   rO   )FONTNAMErM   rN   	Helvetica)FONTSIZErM   rN   rF   )ALIGNrM   rN   CENTER)VALIGNrM   rN   MIDDLE)
TOPPADDINGrM   rN      )BOTTOMPADDINGrM   rN   rX   )LEFTPADDINGrM   rN   rX   )RIGHTPADDINGrM   rN   rX      zCustomer Research:z=Define Research Methodology (Sample Size, Probes, and Method)z[What are some key observations around shopping behaviours around categories by customers?  zCategory Management)zStore Formatz Level of Category or MerchandisezNumber of Categories Availablelowmediumhigh0)rG   rG   rG   )r0      ra      )rP   rM   )rO   r   r;   )rP   )r   r5   rN   rQ   )rW   rM   rN      )rY   rM   rN   rc   zCategory Role Management)Category NamezCategory RolezSales ContributionzMargin Contributionc              3   4   K   | ]  }|j                     y w)N)gross_id).0gms     r'   	<genexpr>z*download_module4_report.<locals>.<genexpr>   s     4mKlRR[[Kls   Flagship#   zCash Machine   Destination   zCore TrafficMaintainz
Under Firez	Category z.1f%   )rG   rG   rG   rG   )   rr   rr   rr   )rR   rM   rN   r6   zGross Margin Contributions)rd   zContribution to SaleszGross MarginzGross Margin ContributionFTd   z.2f) rt   rt   rt   	SmallWraprX   CJK)r<   r=   leadingwordWrapz#Total Weighted Average Gross Marginrt   )rP   rM   rN   r;   zapplication/pdfzContent-Dispositionz+attachment; filename=module_4_report_group_z.pdf)
media_typeheadersr   r   )0r   _fetch_module4_datar   r
   r	   r   r   hAlignappendr   r   r   competitor_analysisreference_competitorr$   number_of_categories_stockedkey_categories_stockedstocking_observationsr   setStyler   r   blackresearch_methodologyshopping_behaviorstore_formatsstore_format_typemerchandiselowerpre_selected_categories	low_valuemedium_value
high_valuelenr   gross_margin_contributionsany	enumeraterf   contribution_to_total_salescontribution_to_gross_marginrangebuildseekr   readr#   r   )*r   r   r   r%   reportbufferdocstylesstorylogotitle_stylesection_stylecentered_style	comp_datalabelvalue
comp_table	cust_data
cust_tablecat_mgmt_datastore_formatcategory_count	cat_tablecategory_role_datairh   sales_contribmargin_contribrolecategory_role_tablemargin_headersmargin_datatotal_sales_contribtotal_margin_contribtotal_gross_margin_contribhas_datagross_margin_calcmargin_tablesmall_style
total_datatotal_tabler&   s*                                             r'   download_module4_reportr       s8
    R<&r51228<<+-2*,2? %&	c"=D DKLLLL2' %&"4B"24 	Y>LMVAr]#&F:$6B%' 	Y1=ABY5vh7GHIVAr]#'vh/!

 $V%?%?%T%T%]X]^+S1K1K1h1h1mlm-no%v'A'A'X'X'a\ab[]c]w]w  ^N  ^N  ^W  RW  X	
	 &LE5E>2Ie^4TUV*4J
 
1fll;;1563645
, 
! 
 LL$# && 	VAr]#Y3VH5EFGVAr]#LfNhNhN}N}  OG  BG  Hjlr  mG  mG  mY  mY  mb  ]b  c
	
 &LE5E>2Ie^4TUV*4J
 
1fll;;1563645
, 
! 
 LL$# && 	VAr]#Y4mDE pp #00L--,2J2J++113u<%()G)G)Q)Q)VUV%WN!--335A%()G)G)T)T)YXY%ZN!--335?%()G)G)R)R)WVW%XN%(N$$ 22 ,,"&  1& }"  !67-?PRtVYZgVhOhi	:VXq&,,7;7-12/2	'
 	 		 	YVAr]# 	Y[!Y9=IJ nn,,4m6KlKl4m1m"6#D#DE2;;$&$B$B$GaM%'%D%D%IN %*~/C)'2--&",,&",-&",)+&--#AaC5)(-Q/)#.a0	/ ' F6 1X"))*FG  $$6BVdfcgjm  oA  kB  dB  C$$ZVXq&,,7;7.12/2	1
 	& 		 	()VAr]#Y;]KLp%&   %&"v@@AEAr{{ " > > C!!#!@!@!EAN[`n]^%Cs$Jtu!#}4#$6$*.??*""	!A#/@s 315~F 4A6G!23 7q9>J	$  B& 1X""#34  [4HVXUY\_`k\lUlmjVXq&,,7;7.12/201*
  	 	\"VAr]# %x 0U

 ;[I-5"3'q)2.6#C(*B4<)#.a0"	
 
 J2FTVSWXZVXq&,,7<.12/2)
  	 	[!		%A FKKM"(*.YZbYccg,hi
 	
S =	~  <CF;;<sm   e9 e e/e Ae
 Le &e 3He E'e 3He e9e 
ee 	e6e11e66e9z/{group_id}/preview-report-pdf4c                    K   	 t        ||      }|j                  |        d{   }| |dS 7 	# t        $ r  t        $ r}t        ddt	        |             d}~ww xY ww)z4Generate PDF report and return file path informationN)r   	file_pathr   zError generating PDF: r   )r   generate_and_save_pdfr   r#   r$   )r   r   r   r%   r   r&   s         r'   generate_and_serve_pdfr   ;  s{     W&r51!77AA	 !"
 	
 B   W6LSQRVH4UVVWs1   A# 0 .0 A#0 A AA  A#N)(fastapir   r   r   fastapi.responsesr   sqlalchemy.ormr   ior   reportlab.lib.pagesizesr	   reportlab.platypusr
   r   r   r   r   r   r   reportlab.lib.stylesr   r   reportlab.libr   app.database.main.mysqlr   app.dependency.authanticationr   r   app.api.module_4_report.servicer   app.api.module_4_report.schemar   module_4_report_routergetintr(   r   r        r'   <module>r      s0   5 5 / "  & h h h D   * O @ ?"  2CWX &/%&9:
<
<
< 
< Y
< 34 &/%&9:W<W<W< W< 5W<t => &/%&9:WWW W ?Wr   