
    .i                         d dl 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
c mc 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 d d	lmZ  G d
 d      Zy)    N)List)HTTPException)load_workbook)Session)Messages)CustomerLocationBaseTblCustomerLocation)%calculate_total_household_consumption.fill_and_calculate_total_household_consumptionremove_external_linkssafe_load_workbook)CustomResponsec                       e Zd ZdefdZdeej                     fdZde	fdZ
de	fdZde	d	ej                  fd
Zdej                  fdZde	fdZy)Customer_locationdbc                      || _         || _        y N)r   token)selfr   r   s      H/var/www/html/retail-simulation-api/app/api/customer_location/service.py__init__zCustomer_location.__init__   s    
    requestc                 *  K   |D ]  }t        j                  |j                               }t        |      |_        t        j                  || j                         | j                  j                          d}d}t        ||      }t        |       |j                  |       |d   }|j                  |d<   |d   }|j                  |d<   |j                  |       |j                          t        d|        t        dt         j"                        S w)	N;uploaded_files/Simulation_Test_file_clean_final_output.xlsx+files/Simulation_Test_file_clean_final.xlsxAssu Sum Mod AK11Assu Sum Mod B"   ✅ Excel updated successfully at:1statusmessage)r   model_validate
model_dumpr
   %total_household_consumption_per_monthr	   creater   commitr   r   saveincome_levelscloseprintr   r   CUSTOMER_CREAT)	r   r   reqcreated_userOUTPUT_PATH
EXCEL_PATHworkbooksheet_asheet_bs	            r   create_customer_locationz*Customer_location.create_customer_location   s     C/>>s~~?OPLAfgsAtL>&&|TWW=GGNNWKFJ)+zBH!(+MM+& /0G ..GEN/0G ..GENMM+&NN6D; < S(2I2IJJs   DDcustomer_location_idc                    K   t        j                  || j                        }|st        dt        j
                        t        j                  j                  |      S w)N  status_codedetail)	r	   	get_by_idr   r   r   CUSTOMET_NOT_FOUNDschemascustomerLocationResponser%   )r   r7   	catchments      r   get_customer_locationz'Customer_location.get_customer_location2   sJ     '112FP	Cx7R7RSS//>>yIIs   AAgroup_idc                   K   | j                   j                  t              j                  t        j                  |k(        j                         }|D cg c]!  }t        j                  j                  |      # c}S c c}w wr   )	r   queryr	   filterrC   allr?   r@   r%   )r   rC   customer_locationslocs       r   get_customer_locations_by_groupz1Customer_location.get_customer_locations_by_group9   sf     "ggmm,?@GGH[HdHdhpHpquuwPbcPb00??DPbcccs   AB&A=:Breturnc                 0  K   | j                   j                  t              j                  t        j                  |k(        j                         }|st        dt        j                        t        d |D              }t        j                  ||      S w)Nr9   r:   c              3   N   K   | ]  }|j                   |j                  z    y wr   )shopping_frequency_per_monthhousehold_consumption_per_month).0rI   s     r   	<genexpr>zACustomer_location.get_total_potential_by_group.<locals>.<genexpr>A   s%     |r{kn @ @3CfCf fr{s   #%)rC   r'   )r   rE   r	   rF   rC   rG   r   r   CUSTOMER_GROUP_NOT_FOUNDsumr?   TotalConsumptionResponse)r   rC   	locationstotal_consumptions       r   get_total_potential_by_groupz.Customer_location.get_total_potential_by_group=   sv     WW]]#67>>?R?[?[_g?ghlln	C8Y8YZZ|r{||//hyzzs   BBc                   K   |j                   t        dt        j                        t	        j
                  |j                   | j                        }|st        dt        j                        |j                  d      }t        ||      }t        di |}|j                   J t	        j                  |j                   || j                         | j                  j                          d}d}t        ||      }t        |       |j                  |       |d   }|j                   |d<   |d	   }	|j                   |	d<   |j                  |       |j#                          t%        d
|       t'        dt        j(                        S w)Nr9   r:   T)exclude_unsetr   r   r   r   r   r    r!   r"    )r7   r   r   CUSTOMER_ID_REQUIREDr	   r=   r   r>   r&   r   r   updater)   r   r   r*   r+   r,   r-   r   CUSTOMER_UPDATE)
r   r   existingupdate_datavalidated_modelr1   r2   r3   r4   r5   s
             r   update_customer_locationz*Customer_location.update_customer_locationD   sW    ''/C8U8UVV&001M1MtwwWC8S8STT((t(<D[RZ[.==33???""?#G#GZ^ZaZabSB
%k:>h'k" +, ..+, ..k"2K@S(2J2JKKs   FF	c                    K   t        j                  || j                        }|st        dt        j
                        t        dt        j                        S w)Nr9   r:   r!   r"   )r	   deleter   r   r   r>   r   CUSTOMER_DELETED)r   r7   deleteds      r   delete_customer_locationz*Customer_location.delete_customer_locationk   sF     %,,-A477KCx7R7RSSS(2K2KLLs   AAN)__name__
__module____qualname__r   r   r   r?   customer_location_creatr6   intrB   rJ   rT   rW   customerLocationUpdatera   rf   rZ   r   r   r   r      s    7 Kd7;Z;Z6[ KBJ Jdc d{3 {7CcCc {%Lg6T6T %LNM3 Mr   r   )ostypingr   fastapir   openpyxlr   sqlalchemy.ormr   !app.api.customer_location.schemasapicustomer_locationr?   app.locale.messagesr   !app.models.main.customer_locationr   r	   app.utils.common_utilsr
   r   r   r   app.utils.schemas_utilsr   r   rZ   r   r   <module>ry      s=    	  ! " " 3 3 ( V d  d 2cM cMr   