
    J)i/                        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 d dlmZmZmZmZ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  e       Z e jC                  de       ee       ee      fde"dedefd       Z#e jC                  d       ee       ee      fde"dedefd       Z$e jC                  d       ee       ee      fde"dedefd       Z%y)    )	APIRouterDependsResponse)Session)BytesIO)A4)colors)getSampleStyleSheet)SimpleDocTemplate	ParagraphSpacerTable
TableStyleImage	PageBreak)Module6ReportPreview)Module6ReportService)ParagraphStyle)get_db)JWTPayloadSchemaget_current_studentz/{group_id}/preview6)response_modelgroup_iddbtokenc                 X   K   t        ||      }|j                  |        d {   S 7 w)N)r   _fetch_module6_data)r   r   r   services       E/var/www/html/retail-simulation-api/app/api/module_6_report/router.pypreview_reportr       s+      #2u-G,,X6666s   !*(*z/{group_id}/download6c                 Z  K   t        ||      }|j                  |        d {   }t               }t        |t        dddd      }g }t               }t        d|d   dddd	      }		 t        d
dd      }
d|
_        |j                  |
       |j                  t        dd             |j                  t        d|	             |j                  t        dd             t        d|d   ddd      }t        d|d   ddd      }t        d|d   dddd      }d }|j                  t        d|             |j                  t        dd             t        d|      t         ||j                  j                        |      gt        d|      t         ||j                  j                        |      gt        d |      t         ||j                  j                        |      gt        d!|      t         ||j                  j                         |      gt        d"|      t         ||j                  j"                        |      gg}t%        |dd#g$      }|j'                  t)        d%d&d'dt*        j,                  fd(d)d*d+d,d-d.g             |j                  |       |j                  t        dd             |j                  t        d/|             |j                  t        dd             t/        d0 |j0                  D        d       }t/        d1 |j0                  D        d       }|sT|sRt3        |j0                        d2kD  r|j0                  d2   nd }t3        |j0                        dkD  r|j0                  d   nd }t        d3|      t        d|      t        d4|      gt        d5|      t        |r ||j4                        nd6|      t        |r ||j6                        nd6|      gt        d7|      t        |r ||j4                        nd6|      t        |r ||j6                        nd6|      gg}t%        |g d8$      }|j'                  t)        d%d&d'dt*        j,                  fd(d)d+d,d-d.g             |j                  |       |j                  t        dd             |j                  t9                      |j                  t        d9|             |j                  t        dd             t        d|      t         ||j:                  j                        |      gt        d:|      t         ||j:                  j<                        |      gt        d;|      t         ||j:                  j>                        |      gt        d<|      t         ||j:                  j@                        |      gt        d"|      t         ||j:                  jB                        |      gg}t%        |d=d=g$      }|j'                  t)        d%d&d'dt*        j,                  fd(d)d+d,d-d.g             |j                  |       |j                  t        dd             |j                  t        d>|             |j                  t        dd             t        d?|      t        d@|      t        dA|      t        dB|      t        dC|      t        dD|      gg}g }|jD                  D ]  }|j                  t         ||jF                        |      t         ||jH                        |      t         ||jJ                        |      t         ||jL                        |      t         ||jN                        |      t         ||jP                        |      g        t%        ||z   g dE$      }|j'                  t)        d%d&d'dt*        j,                  fd(d)dFdGdHdIg             |j                  |       |j                  t        dd             |jR                  rA|j                  t        dJ|             |j                  t        dd             t        d@|      t        d|      t        d4|      gg}g }|jR                  D ]d  }|j                  t         ||jH                        |      t         ||jT                        |      t         ||j6                        |      g       f t%        ||z   g dK$      }|j'                  t)        d%d&d'dt*        j,                  fd(d)d+d,d-d.g             |j                  |       |j                  t        dd             |jW                  |       |jY                  d2       t[        |j]                         dLdMdN|  dOiP      S 7 	}#  Y xY ww)QN2   )pagesize
leftMarginrightMargin	topMarginbottomMarginTitleHeading1         zHelvetica-Bold)parentfontSize
spaceAfter	alignmentfontNamezTS Logo.png   K   )widthheightLEFT
   zModule 6: Promotions   SectionHeaderHeading2   )r-   r.   r/   r1   	CellStyleNormalCJK)r-   r.   leadingwordWrapHeaderStyle)r-   r.   r?   r1   r@   c                 $    | dvrt        |       S dS )N)N N/Ar   rD   )str)values    r   safe_strz!download_report.<locals>.safe_strZ   s    "*>>s5zIEI    z'Competitor Analysis on Pricing StrategyzReference CompetitorzPricing StrategyzHighest Income CategoryzKey ObservationszOther Remarksi^  )	colWidthsGRIDr   r   rM   )VALIGNrK   rL   TOP)FONTNAMErK   rL   	Helvetica)FONTSIZErK   rL   r7   )LEFTPADDINGrK   rL      )RIGHTPADDINGrK   rL   rT   )
TOPPADDINGrK   rL   rT   )BOTTOMPADDINGrK   rL   rT   z Pricing Strategy by Store Formatc              3   P   K   | ]  }d t        |j                        v s|   yw)ANrE   store_format_type.0sfs     r   	<genexpr>z"download_report.<locals>.<genexpr>|   '     l"GB3RUVXVjVjRkKkR"G   &&c              3   P   K   | ]  }d t        |j                        v s|   yw)BNrZ   r\   s     r   r_   z"download_report.<locals>.<genexpr>}   r`   ra   r   zStore Format	RationalezStore Format ArD   zStore Format B)x         z!Competitor Analysis on PromotionszOngoing Key PromotionszdAmong the various promotions, which one do you think is the most effective/useful for the competitorz>What are some key observations around promotions in the store?   zYearly Promotion ListMonthCategoryzType of PromotionzSuccess Metric	ObjectiveRemarks)<   P   rn   rn   rn   re   )rS   rK   rL      )rU   rK   rL   ro   )rV   rK   rL   ro   )rW   rK   rL   ro   zCategory Pricing Strategies)re   re   i  zapplication/pdfzContent-Dispositionz8attachment; filename=module6_pricing_promotions_loyalty_z.pdf)content
media_typeheaders)/r   r   r   r   r   r
   r   r   hAlignappendr   r   competitor_pricing_analysisreference_competitorpricing_strategy6which_category_generates_the_most_income_for_the_store+key_observation_around_pricing_in_the_storeother_remarksr   setStyler   r	   blacknextstore_format_pricing_strategislenchoose_a_pricing_strategiesrationalr   define_your_promotions_strategyongoing_key_promotionsamoung_key_variouse_promotionwhat_are_the_same_keyother_remarkpromotional_calendarmonthcategorytype_of_promotionsuccess_metric	objectiveremarkscategory_pricing_strategispricing_strategisbuildseekr   getvalue)r   r   r   r   reportbufferdocelementsstylestitle_stylelogosection_style
cell_styleheader_stylerG   pricing_datapricing_tableformat_aformat_bstore_format_datastore_format_tablepromotions_datapromotions_tableyearly_promotion_headersyearly_promotion_rowspromoyearly_promotion_tablecategory_headerscategory_rowsr   category_tables                                  r   download_reportr      s
     #2u-G..x88FYF
FRBBZ\km
nCH "F !j!!K]#b9q"& OOI4kBCOOF1bM" #j!!M  hJ "h!LJ OOIGWXOOF1bM" 
)<	8)HVMoMo  NE  NE  EF  HR  ;S  	T	%|	4iIkIkI|I|@}  @J  7K  	L	,l	;YxPVPrPr  Qj  Qj  Hk  mw  >x  	y	%|	4iIkIk  JX  JX  AY  [e  7f  	g	?L	19XfFhFhFvFv=w  zD  4E  	FL ,3*=M:	1fll3+3*,-+.	' 	 	 OOM"OOF1bM" OOI@-PQOOF1bM" l&"G"GlnrsHl&"G"GlnrsH H?B6ChCh?ilm?m688;sw?B6ChCh?ilm?m688;sw
 
><	0	%|	4	;	-	/ 
#Z	0	X8H@@ASXZd	e	(8H--.z	R	T 
#Z	0	X8H@@ASXZd	e	(8H--.z	R	T 0OL
	1fll3+3,-+., !  OO&'OOF1bM" OOIK OOIA=QROOF1bM" 
)<	8)HVMsMs  NI  NI  EJ  LV  ;W  	X	+\	:IhvOuOu  PM  PM  GN  PZ  =[  	\	y  |H  
I  KT  U]  ^d  ^D  ^D  ^b  ^b  Uc  eo  Kp  	q	SUa	bdmnvw}  x^  x^  xt  xt  ou  wA  eB  	C	?L	19XfFlFlFyFy=z  }G  4H  	IO _c
Cj	1fll3+3,-+.*   OO$%OOF1bM" OOI5}EFOOF1bM" 
7L	)	:|	,	&	5	#\	2	;	-	9l	+	-  ,,$$hu{{+Z8hu~~.
;hu667Dhu334jAhu/<hu}}-z:&
 	 - ##;>S#S_xy##J	1fll3+3,-+.0 %  OO*+OOF1bM" ((	"?OPq"& z<0)<NP\2]_hit  wC  `D  E
 99H  (8#4#45zB(8#=#=>
K(8#4#45zB"  : /-??[
VXq&,,7/701/2,
 ! 	 	'q"&IIh
KKN!$&*bckbllp(qr C 9,s*   !f+f Af+(Af# )c8f+#f(%f+z/{group_id}/preview-report-pdf6c                    K   	 t        ||      }|j                  |        d{   }| |dS 7 	# t        $ r!}ddlm}  |ddt        |             d}~ww xY ww)z4Generate PDF report and return file path informationN)r   	file_pathr   )HTTPExceptioni  zError generating PDF: )status_codedetail)r   generate_and_save_pdf	Exceptionfastapir   rE   )r   r   r   r   r   er   s          r   generate_and_serve_pdfr   
  ss     W&r51!77AA	 !"
 	
 B  W)6LSQRVH4UVVWs0   A 0 .0 A0 	AAAAN)&r   r   r   r   sqlalchemy.ormr   ior   reportlab.lib.pagesizesr   reportlab.libr	   reportlab.lib.stylesr
   reportlab.platypusr   r   r   r   r   r   r   app.api.module_6_report.schemar   app.api.module_6_report.servicer   r   app.database.main.mysqlr   app.dependency.authanticationr   r   module_6_report_routergetintr    r   r    rH   r   <module>r      s0   0 0 "  &   4 h h h ? @ / * O"  2CWX &/%&9:777 7 Y7 34 &/%&9:kkk k 5k\ => &/%&9:WWW W ?WrH   