
    J)i                        d dl 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 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m Z   e       Z!e!jE                  de       ee       ee       fde#dedefd       Z$e!jE                  d       ee       ee       fde#dedefd       Z%e!jE                  d       ee       ee       fde#dedefd       Z&y)    )	APIRouterDependsResponseHTTPException)Session)BytesIO)A4)colors)getSampleStyleSheetParagraphStyle)SimpleDocTemplate	ParagraphSpacerTable
TableStyleImage	PageBreak)Module2ReportPreview)Module2ReportService)get_db)JWTPayloadSchemaget_current_studentz/{group_id}/preview2)response_modelgroup_iddbtokenc                 X   K   t        ||      }|j                  |        d{   S 7 w)z.Preview Module 2 Location Analysis report dataN)r   _fetch_module2_data)r   r   r   services       >/var/www/html/retail-simulation-api/app/api/module_2/router.pypreview_reportr!      s+      #2u-G,,X6666s   !*(*z/{group_id}/download2c                   K   t        ||      }|j                  |        d{   }t               }t        |t        dddd      }g }t               }t        d|d   d      }	|d   }
|d	   }	 t        d
dd      }d|_        |j                  |       |j                  t        dd             |j                  t        d|	             |j                  t        dd             |j                  t        d|
             |j                  t        d|             |j                  t        d|             |j                  t        d|             |j                  t        dd             |j                  t        d|
             |j                  t        dd             g dg}|j                  D ]U  }|j                  t        |j                  xs d      |j                  xs d dt        |j                   xs d      g       W t#        |g d      }|j%                  t'        ddd dt(        j*                  fd!d"d#d$d%d&d'g             |j                  |       |j                  t        dd             t-        |d(d      }|j                  t        d)| d|             |j                  t        dd             |j                  t        d*|
             |j                  t        d+|             t-        |d,d-      }|j                  t        | |             |j                  t        dd             |j                  t        d.|
             |j                  t        d/|             t-        |d0d-      }|j                  t        | |             |j                  t        dd             |j                  t        d1|
             |j.                  D ]  }|j0                  xs d2}|j                  t        d3| |             |j                  t        d4|j2                  xs d |             |j                  t        d5|             |j                  t        d6|             |j                  t        d7|             |j                  t        d8|             |j                  t        d9|             |j                  t        dd:              |j                  t        dd             |j                  t5                      |j                  t        d;|
             |j                  t        dd             g d<g}t-        |d=d      }t-        |d>d      }|j                  dd?|d@d?|d@g       t#        |g dA      }|j%                  t'        ddd dt(        j*                  fd!d"d#d$d%d&d'g             |j                  |       |j                  t        dd             |j                  t        dB|
             |j6                  D ]  }|j0                  xs d2}|j                  t        d3| |             |j                  t        dC|j8                  xs dD |             |j                  t        dE|j:                  xs dD |             |j                  t        dF|j<                  xs dD |             |j                  t        dd:              |j                  t        ddG             |j?                  |       |jA                  dD       dH|  dI}tC        |jE                         dJdKdL| iM      S 7 /#  Y xY ww)Nz1Download Module 2 Location Analysis report as PDFN(   )pagesize
leftMarginrightMargin	topMarginbottomMarginCustomTitleHeading2   )parent	alignmentHeading4NormalzTS Logo.png   K   )widthheightLEFT
   zModule 2: Location Analysis   z<b>Understanding Shoppers</b>u    • Shopping Frequency per Monthu#   • Household Consumption per Monthu)   • Total Household Consumption per Month   z<b>Catchment Potential</b>)zISEC CategoryzPercentage of SegmentzNumber of Potential Households g        %)i   r0      )	colWidthsGRIDr   r   r@   )FONTNAMEr>   r?   	Helvetica)ALIGNr>   r?   r4   )VALIGNr>   r?   TOP)LEFTPADDINGr>   r?      )RIGHTPADDINGr>   r?   rG   )BOTTOMPADDINGr>   r?      )
TOPPADDINGr>   r?   rJ   estimated_market_sharezEstimated Market Share: z<b>Competition Intensity</b>u(   • Total Square foot of the competitiontotal_sqft_of_competitionzN/Az<b>Quality of Location</b>u   • Spillage Factorspillage_factorz<b>Location Definition</b>AzStore Format: u   • Trading Radius: u   • Adjacenciesu   • Corner Propertyu   • Highstreetu   • Mallu   • Parking SpacerJ   z<b>Estimated Sales</b>)zEstimated SaleszStore AzStore Bestimated_sales_aestimated_sales_bzRs.z,.2f)x   rR   rR   z<b>Rent Details</b>u'   • Rental Advance Amount (in Rupees): r   u'   • Rental Advance Period (in Months): u    • Rent per Month (in Rupees): rG   module2_location_analysis_z.pdfzapplication/pdfzContent-Dispositionzattachment; filename=)content
media_typeheaders)#r   r   r   r   r	   r   r   r   hAlignappendr   r   catchment_potentialstrISEC_Segmentpercentage_of_segmentpotential_number_of_householdsr   setStyler   r
   blackgetattrlocation_detailsstore_format_typetrading_radiusr   rent_expensesrental_advance_amountrental_advance_periodrent_per_monthbuildseekr   getvalue)r   r   r   r   reportbufferpdfelementsstylestitle_stylesection_stylenormallogocatchment_dataccatchment_table
est_markettotal_competitionspillagelocationfmt
sales_datasale_asale_bsales_tablerentfilenames                              r    download_reportr      s     #2u-G..x88FYF
FRBBZ\km
nCH "F j!K
 :&MHF]#b9q"&
 OOI;[IJOOF1bM" OOI=}MNOOI@&IJOOICVLMOOII6RSOOF1aL! OOI:MJKOOF1bM"bcN''$"%&&-#.a0006B7
 	 ( NoFOZ	1fll33++,-.+	) 	 	 OOO$OOF1bM"!93?JOOI 8AFOPOOF1bM" OOI<mLMOOIH&QR(CUKOOI!2 3f=>OOF1aL! OOI:MJKOOI3V<=v0%8HOOI
V45OOF1aL! OOI:MJK++((/C	N3%"8&AB	$89P9P9WTW8X"Y[abc	"3V<=	"7@A	"2F;<	*f56	"5v>?q!% , OOF1aL! OOIK OOI6FGOOF1bM";<JV0#6FV0#6FrS.#fT]0CDE
o>K	1fll33++,-.+	% 	 	 OOK OOF1bM" OOI3]CD$$$$+	N3%"8&AB	$KDLfLfLkjkKl"mouvw	$KDLfLfLkjkKl"mouvw	$DTEXEXE]\]D^"_aghiq!% % OOF1aL!IIh
KKN+H:T:H!$&*?z(JK } 9(s*   !]]A
]/A] 0Z#]]]z/{group_id}/preview-report-pdf2c                    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_pathi  zError generating PDF: )status_codedetail)r   generate_and_save_pdfr   	ExceptionrZ   )r   r   r   r   r   es         r    generate_and_serve_pdfr      s{     W&r%0!77AA	 !"
 	
 B   W6LSQRVH4UVVWs1   A# 0 .0 A#0 A AA  A#N)'fastapir   r   r   r   sqlalchemy.ormr   ior   reportlab.lib.pagesizesr	   reportlab.libr
   reportlab.lib.stylesr   r   reportlab.platypusr   r   r   r   r   r   r   app.api.module_2.schemar   app.api.module_2.servicer   app.database.main.mysqlr   app.dependency.authanticationr   r   module_2_report_routergetintr!   r   r        r    <module>r      s-   ? ? "  &   D h h h 8 9 * O"  2CWX &/%&9:777 7 Y7 34 &/%&9:III I 5IX => &/%&9:WWW W ?Wr   