
    .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m	Z	 d dl
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  G d d      Zy)    N)List)load_workbook)schema)OperatingExpensesBaseTblOperatingExpenses)remove_external_linkssafe_load_workbook)CustomResponse)Session)JWTPayloadSchema)Messages)HTTPExceptionc                   z    e Zd ZdedefdZdej                  fdZde	fdZ
deej                     fdZd	e	fd
Zy)OperatingExpensesServicedbtokenc                      || _         || _        y )N)r   r   )selfr   r   s      I/var/www/html/retail-simulation-api/app/api/operating_expenses/service.py__init__z!OperatingExpensesService.__init__   s    
    requestc                 \  K   t        j                  |j                               }t        j                  || j
                         | j
                  j                          d}d}t        ||      }t        |       |j                  |       ddd}ddd}|j                         D ]G  \  }}	||	   }
|j                  |k(  s|j                  |j                        }|s9|j                  |
|<   I |j                  |       |j                          t!        d	t"        j$                  
      S w)N;uploaded_files/Simulation_Test_file_clean_final_output.xlsx+files/Simulation_Test_file_clean_final.xlsxI15I17zMaintenance and RepairszAdmin OverheadsAssu Sum Mod AAssu Sum Mod BzStore Format AzStore Format B1statusmessage)r   model_validate
model_dumpr   create_operating_expensesr   commitr	   r   saveitemsstore_format_typegetoperating_expenses_typepercentage_of_sales_per_yearcloser
   r   OPERATING_EXPENSES)r   r   created_operating_ExpensesOUTPUT_PATH
EXCEL_PATHworkbookparam_to_cellformat_to_sheetformat_type
sheet_namesheetcells               r   create_operating_Expensesz2OperatingExpensesService.create_operating_Expenses   s    %:%I%I'J\J\J^%_"667QSWSZSZ[SB
%k:>h'k" (-$

 /.
 (7'<'<'>#KZ(E((K7$(()H)HI"<"Y"YE$K (? 	k"S(2M2MNNs   B>D,D,AD,group_idc                    K   t        j                  || j                        }|st        dd      |D cg c]!  }t        j
                  j                  |      # c}S c c}w w)Ni  zGet groupID not found)status_codedetail)r   get_operating_expensesr   r   r   OperatingExpensesResponser&   )r   r=   new_get_operating_Expenses	get_groups       r   get_operating_Expensesz/OperatingExpensesService.get_operating_Expenses<   s]     %9%P%PQY[_[b[b%c")C8OPP\vw\vy00??	J\vwwws   4A$&AA$c                 
  K   d }|D ]y  }t        j                  |j                               }|j                  t	        dt
        j                        c S t        j                  |j                  || j                        }{ | j                  j                          |r| j                  j                  |       d}d}t        ||      }t        |       |j                  |       ddd}dd	d
}	|D ]R  }|	j                  |j                         }
|
s!||
   }|j                  |j"                        }|sD|j$                  ||<   T |j                  |       |j'                          t	        dt
        j(                        S w)N-1r#   r   r   r   r   r   r   r    r!   r"   )r   r&   r'   operating_idr
   r   OPERATING_EXPENSES_NOTr   update_operating_expensesr   r)   refreshr	   r   r*   r-   r,   r.   r/   r0   OPERATING_EXPENSES_UPDATE)r   r   last_db_rowrequpdated_operating_expensesr3   r4   r5   r6   r7   r9   r:   	cell_addrs                r   rJ   z2OperatingExpensesService.update_operating_expensesC   s`    C)>)M)McnnN^)_&)66>%T8;Z;Z[[.HH*77*K  	GGOOK(SB
%k:>h'k" (-$
 /.
 C(,,S-B-BCJZ(E%))#*E*EFI#&#C#Ci   	k"S(2T2TUUs   D3F6AFrH   c                    K   t        j                  || j                        }|st        dt        j
                        S t        dt        j                        S w)NrG   r#   r"   )r   delete_operatin_expensesr   r
   r   rI   OPERATING_EXPENSES_DELETE)r   rH   deleted_operating_expensess      r   delete_operating_expensesz2OperatingExpensesService.delete_operating_expenses{   sJ     %9%R%RS_aeahah%i")!x7V7VWWS(2T2TUUs   AAN)__name__
__module____qualname__r   r   r   r   OperatingExpensesCreater<   intrE   r   OperatingExpensesUpdaterJ   rU    r   r   r   r      sa    ' )9 'Of6T6T 'ORxC x5VtF<Z<Z7[ 5VpV3 Vr   r   )ostypingr   openpyxlr   app.api.operating_expensesr   "app.models.main.operating_expensesr   r   app.utils.common_utilsr   r	   app.utils.schemas_utilsr
   sqlalchemy.ormr   app.dependency.authanticationr   app.locale.messagesr   fastapir   r   r\   r   r   <module>rh      s4    	  " - Z L 2 " : ( !qV qVr   