
    H(i                        d dl mZ d dl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 d dlmZ dd	lmZmZmZ d d
lmZmZ d dlZd dlZd dlmZ  e       Z e jC                  dd       ee       ee      fdede
defd       Z"e jC                  dd       e        ed       ee       ee      fdedede
defd       Z#e jI                  dd       e        ed       ee       ee      fde%dedede
def
d       Z&e jO                  dd       ee       ee      fde%de
defd       Z(e jS                  dd       ee       ee      fde%de
defd       Z*dejV                  d e,fd!Z-e jC                  d"       ed#d$%       ee       ee      fde%de
defd&       Z.e jC                  d'       ed#d(%       ee       ee      fde%de
defd)       Z/y)*    )Optional)	APIRouterDependsFileFormQuery
UploadFile)Session)service)get_db)JWTPayloadSchemaget_current_student)TblStoreFormat   )StoreFormateCreateStoreFormateUpdateStoreFormateCreate11)JSONResponseStreamingResponseN)FPDFz/store-formateT)response_model_exclude_nonerequestdbtokenc                 h   K   t        j                  ||      j                  |        d {   S 7 wN)r   StoreFormateServicecreate_store_formate)r   r   r   s      C/var/www/html/retail-simulation-api/app/api/store_formate/router.pyr   r      s*     ,,R6KKGTTTT   )202z/store-formatesupload_store_layoutc                 R    t        j                  ||      }|j                  | |      S r   )r   r   create_store_format3)r   r!   r   r   service_instances        r   create_store_formate111r%      s)    222u=00:MNN    z/store-formate/updatestore_idc                 p   K   t        j                  ||      }|j                  | ||       d {   S 7 wr   )r   r   update_store_formate)r'   r   r!   r   r   r$   s         r   update_store_formatr*      s7      222u=!66xJ]^^^^s   -646z/store-formate/{group_id}group_idc                 h   K   t        j                  ||      j                  |        d {   S 7 wr   )r   r   get_store_formate)r+   r   r   s      r   r-   r-   )   s*     ,,R7II(SSSSr    z /store-formate/delete/{store_id}c                 h   K   t        j                  ||      j                  |        d {   S 7 wr   )r   r   delete_store_formate)r'   r   r   s      r   delete_storer0   -   s*     ,,R7LLXVVVVr    dfreturnc           	         t               }|j                          |j                  dd       |j                  t	        | j
                        dz   z  }|j                  dz  }| j
                  D ]  }|j                  |||d        |j                  |       | j                         D ];  \  }}|D ]   }|j                  ||t        |      d       " |j                  |       = |j                  d      j                  d	      S )
NArial
   )sizer   g      ?)borderS)destlatin1)r   add_pageset_fontwlencolumns	font_sizecelllniterrowsstroutputencode)r1   pdf	col_width
row_heightcol_name_rowitems           r   df_to_pdf_bytesrN   2   s    
&CLLNLLrL"RZZ1,-I$J JJJ; FF: ++-3DHHY
CIaH@ z  
 ::3:&&x00r&   z/store-formate/download.zGroup ID to filter data)descriptionc                   K   |j                  t              j                  t        j                  | k(        j	                         }|st        dd|  dd      S |D cg c]  }|j                          }}t        j                  |      }t        |      }t        j                  |      }|j                  d       t        |ddd	|  d
i      S c c}w w)N0z+No store format records found for group_id .statusmessagecontentr   zapplication/pdfContent-Disposition(attachment; filename=store_format_group_z.pdf
media_typeheaders)queryr   filterr+   allr   as_dictpd	DataFramerN   ioBytesIOseekr   )	r+   r   r   all_recordsrecorddatar1   	pdf_bytes
pdf_streams	            r   download_store_formate_by_grouprk   H   s      ((>*11.2I2IX2UVZZ\KsAlmulvvw?x$yzz ,77;FNN;D7	d	B  #II&JOOA $&*RS[R\\`(ab  8s   ACC6A!Cz/store-formate/download-htmlz$Group ID to filter store format datac                   K   |j                  t              j                  t        j                  | k(        j	                         }|st        dd|  dd      S |D cg c]  }|j                          }}t        j                  |      }|j                  d      }d|  d	| d
}t        j                  |      }	|	j                  d       t        |	ddd|  di      S c c}w w)NrQ   zNo records found for group_id rR   rS   rV   F)indexaw  
    <!DOCTYPE html>
    <html>
    <head>
        <title>Store Format Report</title>
        <style>
            table { border-collapse: collapse; width: 100%; }
            th, td { border: 1px solid #dddddd; text-align: left; padding: 8px; }
            th { background-color: #f2f2f2; }
        </style>
    </head>
    <body>
        <h2>Store Format Report (Group ID: z)</h2>
        z
    </body>
    </html>
    r   zapplication/octet-streamrX   rY   z.htmlrZ   )r]   r   r^   r+   r_   r   r`   ra   rb   to_htmlrc   StringIOre   r   )
r+   r   r   recordsrg   rh   r1   html_content	html_pagestreams
             r   download_store_formate_htmlrt   e   s      hh~&--n.E.E.QRVVXGsA_`h_iij?k$lmm+237FNN7D3	d	B::E:*L, -5: 6		 I$ [[#F
KKN-&*RS[R\\a(bc 3 4s   AC'C"6A1C')0typingr   fastapir   r   r   r   r   r	   sqlalchemy.ormr
   app.api.store_formater   app.database.main.mysqlr   app.dependency.authanticationr   r   app.models.main.store_formater   schemasr   r   r   fastapi.responsesr   r   rc   pandasra   fpdfr   store_formate_routerpostr   r%   putintr*   getr-   deleter0   rb   bytesrN   rk   rt    r&   r   <module>r      s    E E " ) * O 8 Q Q = 	   { +MFMfopw  yL  qM U(: Ug U]m U NU ,$O<CIimnris  DK  LR  DS  nu  vI  nJ O%9 O\f O  zA O  [k O PO 1tT #*)&*4j&/%&9:___ $_ 		_
 _ U_ 5SWX9@dkl  eA Tc Tw TQa T YT ?\`a3:6?]dex]y W W WJZ W bW1 1 1, 45#+DE&/%&9:  68 9:#+QR&/%&9:''' ' ;'r&   