
    ]ih                     b   d Z ddlmZ 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mZ 	 dde	ded	ed
eee   ef   fdZde	ded
edz  fdZde	d
efdZde	ded
efdZde	ded
ee   fdZ	 dde	dededz  d
edz  fdZ	 dde	dee   dedz  d
eeef   fdZde	deded
dfdZde	deded
efdZy)ub   Product repository — all DB queries for products (Produk) and their price history (ProdukHarga).    )date)and_funcor_select)AsyncSession)selectinload)ProdukProdukHargadbskiplimitreturnc                 D  K   t        t              j                  t        j                  j	                  d            }| j                  t        t        j                               j                  |j                                      d{   }|j                         }| j                  |j                  t        j                        j                  |      j                  |             d{   }t        |j!                         j#                               |fS 7 7 /w)z[Return a paginated product list with total count (uses a proper COUNT query, no full scan).N)r   r
   where
deleted_atis_executer   countselect_fromsubquery
scalar_oneorder_bynamaoffsetr   listscalarsall)r   r   r   
base_querycount_qtotalresults          E/var/www/html/spbu.com/backend/app/repositories/product_repository.pyget_allr$      s      %%f&7&7&;&;D&ABJJJvdjjl3??
@S@S@UVWWG E::FKK(//5;;EB F  $$&'.. Xs%   BD 
DA#D .D/.D D 	produk_idNc                 B  K   | j                  t        t              j                  t        j                  |k(  t        j
                  j                  d            j                  t        t        j                                     d{   }|j                         S 7 w)zFFetch a single product by primary key, with full price history loaded.N)r   r   r
   r   idr   r   optionsr	   harga_historyscalar_one_or_noner   r%   r"   s      r#   	get_by_idr,      sq     ::v	vyyI%v'8'8'<'<T'B	C	f223	4 F
 $$&&s   BBB	Bc                    K   t        di |}| j                  |       | j                          d{    | j                  |       d{    |S 7 7 w)u;   Create a new product record. Flush only — caller commits.N )r
   addflushrefresh)r   kwargsproduks      r#   creater4   #   sM     fFFF6N
((*
**V
M s!   0AAAAAAr3   c                    K   |j                         D ]  \  }}t        |||        | j                          d{    | j                  |       d{    |S 7 7 w)uK   Update scalar fields on an existing product. Flush only — caller commits.N)itemssetattrr0   r1   )r   r3   r2   keyvalues        r#   updater:   ,   sU     lln
UU# %
((*
**V
M s!   9AAAAAAc                 >  K   | j                  t        t              j                  t        j                  |k(        j                  t        j                  j                                      d{   }t        |j                         j                               S 7 +w)z5Return all price entries for a product, newest first.N)r   r   r   r   r%   r   berlaku_mulaidescr   r   r   r+   s      r#   get_harga_historyr>   5   sq     ::{	{$$	1	2	+++002	3 F
  $$&''s   A-B/B0,Btanggalc                   K   |t        j                         }| j                  t        t              j                  t        t        j                  |k(  t        j                  |k  t        t        j                  j                  d      t        j                  |k\                    j                  t        j                  j                               j                  d             d{   }|j                         S 7 w)zDReturn the price entry active on the given date (defaults to today).N   )r   todayr   r   r   r   r   r%   r<   r   berlaku_sampair   r   r=   r   r*   )r   r%   r?   r"   s       r#   get_current_hargarD   ?   s      **,::{	%%2))W4..2248..'9	

 
+++002	3	q F $$&&s   C&C?(C=)C?
produk_idsc                 r  K   |t        j                         }|si S | j                  t        t              j                  t        t        j                  j                  |      t        j                  |k  t        t        j                  j                  d      t        j                  |k\                    j                  t        j                  j                                      d{   }|j                         j!                         }i }|D ]   }|j                  |vs|||j                  <   " |S 7 Kw)z_Fetch the current active price for each product in one query. Returns {produk_id: ProdukHarga}.N)r   rB   r   r   r   r   r   r%   in_r<   r   rC   r   r   r=   r   r   )r   rE   r?   r"   rows	harga_maprows          r#   get_current_harga_bulkrK   W   s     **,	::{	%%))*5))W4..2248..'9	

 
+++002	3 F >>!D(*I==	)'*Icmm$  )s   C'D7)D5*7D7"D7rC   c           
      R  K   | j                  t        t              j                  t	        t        j
                  |k(  t        j                  j                  d                         d{   }|j                         }|r ||_        | j                          d{    yy7 77 w)u   Set berlaku_sampai on the currently open price entry (flush only).

    # Flush within caller's transaction — caller is responsible for commit/rollback
    N)
r   r   r   r   r   r%   rC   r   r*   r0   )r   r%   rC   r"   currents        r#   close_current_hargarN   v   s      ::{!!%%2**..t4	
 F '')G!/hhj  	s$   A)B'+B#,0B'B%B'%B'r<   c                    K   t        |||      }| j                  |       | j                          d{    | j                  |       d{    |S 7 7 w)u^   Insert a new price entry. Flush only — caller commits (atomically with close_current_harga).)r%   hargar<   N)r   r/   r0   r1   )r   r%   rP   r<   phs        r#   create_hargarR      sO      
y]	SBFF2J
((*
**R.I s!   3AAAAAA)r   2   )N)__doc__datetimer   
sqlalchemyr   r   r   r   sqlalchemy.ext.asyncior   sqlalchemy.ormr	   app.models.productr
   r   inttupler   r$   r,   r4   r:   r>   rD   dictrK   rN   rR   r.       r#   <module>r^      sx   h  . . / ' 2 35
/
/
/,/
/
4<
/' ' '$ '\  \ 6  ( ( (kAR ( >B''!$'/3d{'4'2 EI"&s)6:Tk	#{
>!$6:	*!$<@r]   