
    |Si#                        d 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 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  ed      Z G d dej6                        Z G d dej6                        Zy)z+Serializers for investment and reward APIs.    )annotations)	timedelta)DecimalROUND_HALF_UP)transaction)timezone)serializers)ValidationError)InsufficientBalanceError)Transaction)get_wallet_serviceMAKE_INVESTMENT   )
InvestmentRewardz
0.00000001c                  ,    e Zd ZdZ G d d      ZddZy)InvestmentSerializerz3Create/read investments with tier auto-calculation.c                       e Zd ZeZg dZg dZy)InvestmentSerializer.Meta)idamounttierdaily_reward_rateduration_daysstatus
start_dateend_dateroi_total_amountroi_total_percent
created_at
updated_at)
r   r   r   r   r   r   r   r   r    r!   N)__name__
__module____qualname__r   modelfieldsread_only_fields     M/home/cursorai/projects/telegram-earn/backend/apps/investments/serializers.pyMetar      s    

r)   r+   c                
   | j                   d   j                  }t        j                  |d         }t	        j
                         t        d      z   }|t        |d         z   }t        |d         j                  t        t              }t               }t        j                         5  |j                  j                  j!                         j#                  |j$                        }|j&                  }		 |j)                  |j*                  d|d	|d
    t,        d|j*                         |j9                          |j&                  }t        j                  j;                  ||d   |d
   |d   |d   t        j<                  ||      }t>        j                  j;                  |t>        j@                  |t>        jB                  |	|d|j*                  i       |cddd       S # t.        $ r}
|
j0                  t        t3        |
j0                              n
t        d      }|
j4                  t        t3        |
j4                              n
t        d      }|d}|d}t7        dd| d| gi      d}
~
ww xY w# 1 sw Y   yxY w)z0Derive tier, rate, and schedule from the amount.requestr   r   )daysduration)rounding)pkcredit_balancezInvestment in tier r   F)user_id
point_typer   remarks
trans_typeallow_negativeiidN0z.8fz!Insufficient balance. Available: z, Required: rate)userr   r   r   r   r   r   r   investment_id)r;   transaction_typer   r   balance_beforebalance_afterdata)"contextr;   r   get_tier_infor   nowr   r   quantizeDECIMAL_QUANTIZEr   r   r   atomic	__class__objectsselect_for_updategetr1   r2   deduct_pointr   r   r   	availablestr	requestedr
   refresh_from_dbcreateSTATUS_PENDINGr   TYPE_INVESTMENTSTATUS_COMPLETED)selfvalidated_datar;   	tier_infor   r   investment_amountwallet_serviceuser_lockedr>   erL   rN   available_strrequested_strr?   
investments                    r*   rP   zInvestmentSerializer.create7   sd   ||I&++,,^H-EF	\\^iQ&77
	y/D EE#N8$<=FF} G 
 ,-!..00BBDHHDGGHTK(77N++'NN/,1)F2C1DE.#(# , , '')'66M $++22 %h/v&"+F"3'
3!00%! 3 	J && !,!<!<("33-+%z}}5 '  o "!  , 
9:9PGC$45V]^aVb	9:9PGC$45V]^aVb	#,S/#,S/% %F}oUaboap#q"r 
! "!s3   *AI966G",B,I9"	I6+BI11I66I99JN)rU   dictreturnr   )r"   r#   r$   __doc__r+   rP   r(   r)   r*   r   r      s    =
 
:Dr)   r   c                  P    e Zd ZdZ ej
                  dddd      Z G d d      Zy	)
RewardSerializerz'Read-only serializer for daily rewards.      zinvestment.daily_reward_rateT)
max_digitsdecimal_placessource	read_onlyc                      e Zd ZeZg dZeZy)RewardSerializer.Meta)r   r]   r   reward_dater   calculated_atdistributed_atN)r"   r#   r$   r   r%   r&   r'   r(   r)   r*   r+   rj      s    
 "r)   r+   N)r"   r#   r$   r`   r	   DecimalFieldr   r+   r(   r)   r*   rb   rb   ~   s.    1000-	" "r)   rb   N)r`   
__future__r   datetimer   decimalr   r   	django.dbr   django.utilsr   rest_frameworkr	   rest_framework.exceptionsr
   django_wallet_utils.exceptionsr   apps.transactions.modelsr   utils.walletr   r   modelsr   r   rE   ModelSerializerr   rb   r(   r)   r*   <module>r{      s[    1 "  * ! ! & 5 C 0 < &<( d;66 dN"{22 "r)   