
    soj&                     v   d dl m Z mZ d dlT 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 d d
lmZ d dlmZ  G d de	          Ze                                 	 d dlmZmZ d dlmZ  e ej                              Z ee                    ddd                     dS # e $ r Y dS w xY w)    )datetime	timedelta)*)UpdateN)	BaseModel)	GroupChat)User)BountyLoanSource)BountyLoanStatus)ReservedKeyboardKeys)Screenc                   2   e Zd ZU dZ eeddd          Z eeddd          Z e            Z	 e
            Z e            Z ed          Z e            Z e            Z ed	          Z ed	          Z eej                  Z eedd
dd          Z ed	          Z edd          Z ed	          Zeez  ed<    ed          Z e!ez  ed<    G d d          Z"d dede#fdZ$dedefdZ%d Z&d Z'd Z(defdZ)d Z*de+fdZ,d Z-dS )!
BountyLoanz
    BountyLoan class
    bounty_loanersCASCADE)backref	on_delete	on_updatebounty_borrowersr   )defaultT)nullbounty_loan_groups_chatsRESTRICT)r   r   r   r   
   )
max_lengthr   external_idis_auto_forgivenc                       e Zd ZdZdS )BountyLoan.Metabounty_loanN)__name__
__module____qualname__db_table     /app/src/model/BountyLoan.pyMetar   0   s         r&   r(   Namountupdatec                   K   ddl m} ddlm} 	 ||                                 k    rt          d          | xj        |z  c_        t          j                    | _	        | j        | j
        k    rt          j        | _         || j        |d|d           d {V   || j        |d|           d {V  | j                                         | j                                         | j        t          j        k    rk || j        t'          t(          j                                                  z             r.| j        | j        d	z  k    r|                                  d
| _        |                                  d S )Nr   )add_or_remove_bounty)datetime_is_beforez'Amount is greater than remaining amountF)addr*   raise_error_if_negative_bounty)check_for_loanr*   )days   T)src.service.bounty_servicer,   src.service.date_servicer-   get_remaining_amount
ValueErroramount_repaidr   nowlast_payment_daterepay_amountr   REPAIDstatusborrowerloanersaveEXPIREDdeadline_dater   EnvBOUNTY_LOAN_FORGIVENESS_DAYSget_intr)   forgiver   )selfr)   r*   r,   r-   s        r'   payzBountyLoan.pay3   s     CCCCCC??????	 D--////FGGGf$!)!222*1DK #"M6uV\a
 
 
 	
 	
 	
 	
 	
 	
 	
 #"4;uU[\\\\\\\\\\ K+333"""YC4T4\4\4^4^%_%_%__  4 "dkAo55LLNNN$(D!		r&   returnc                 <    t          || j        | j        z
            S )z
        Get the maximum payable amount between the given amount and the remaining amount
        :param amount: The amount
        :return: The maximum payable amount
        )minr:   r7   )rF   r)   s     r'   get_maximum_payable_amountz%BountyLoan.get_maximum_payable_amount`   s     6D-0BBDDDr&   c                 N    t          | j                                                  S )zX
        Whether the loan is active
        :return: Whether the loan is active
        )r   r<   	is_activerF   s    r'   rM   zBountyLoan.is_activeh   s     
  ,,66888r&   c                      | j         | j        z
  S )zP
        Get the remaining amount
        :return: The remaining amount
        )r:   r7   rN   s    r'   r5   zBountyLoan.get_remaining_amounto   s    
  4#555r&   c                     t           j        | _        t          j                    | _        |                                  dS )z8
        Forgive the loan
        :return: None
        N)r   FORGIVENr<   r   r8   forgiven_dater?   rN   s    r'   rE   zBountyLoan.forgivev   s-    
 '/%\^^		r&   c                 *    t          | j                  S )z<
        Get the status
        :return: The status
        )r   r<   rN   s    r'   
get_statuszBountyLoan.get_status       
  ,,,r&   c                 ^    ddl m}  |t          j        | j        it
          j                  S )z@
        Get the deeplink
        :return: The deeplink
        r   )get_deeplink)infoscreen)src.service.message_servicerW   r   DEFAULT_PRIMARY_KEYidr   PVT_BOUNTY_LOAN_DETAIL)rF   rW   s     r'   rW   zBountyLoan.get_deeplink   sB    
 	=<<<<<|&:DGD0
 
 
 	
r&   c                 *    t          | j                  S )z<
        Get the source
        :return: The source
        )r
   sourcerN   s    r'   
get_sourcezBountyLoan.get_source   rU   r&   c                     | j         t          d          |                                                     | j                   S )zN
        Get the source deeplink
        :return: The source deeplink
        Nz$External id is required for deeplink)r   r6   r`   rW   rN   s    r'   get_source_deeplinkzBountyLoan.get_source_deeplink   s=    
 #CDDD  --d.>???r&   )N).r!   r"   r#   __doc__ForeignKeyFieldr	   r>   r=   BigIntegerFieldr)   
FloatFieldtax_percentager:   r7   IntegerFielddurationDateTimeFieldrA   r9   rR   SmallIntegerFieldr   AWAITING_LOANER_CONFIRMATIONr<   r   
group_chat
message_id	CharFieldr_   r   int__annotations__r   boolr(   r   rG   rK   rM   r5   rE   rT   rW   r
   r`   rb   r%   r&   r'   r   r      s          _&)y  F (I  H _FZ\\N"?$$L#OA...M|~~H!MOOM%4000!Mt,,,M'7'TUUUF *  J 4(((JY"4000F&2l&=&=&=K|#===1B1B11M1M1Md..MMM! ! ! ! ! ! ! !+ + +V + + + +ZE E E E E E9 9 96 6 6  -, - - - -

 

 

-, - - - -@ @ @ @ @r&   r   )MySQLMigratormigrate)db_objr    )r<   rA   F)unique)!r   r   peeweesrc.tg_compatr   resources.EnvironmentEnvironmentrB   src.model.BaseModelr   src.model.GroupChatr   src.model.Userr	    src.model.enums.BountyLoanSourcer
    src.model.enums.BountyLoanStatusr   $src.model.enums.ReservedKeyboardKeysr   src.model.enums.Screenr   r   create_tableplayhouse.migraters   rt   
pw_migrateru   get_db	_migrator	add_index	Exceptionr%   r&   r'   <module>r      s   ( ( ( ( ( ( ( (                 # # # # # # ) ) ) ) ) ) ) ) ) ) ) )       = = = = = = = = = = = = E E E E E E ) ) ) ) ) )R@ R@ R@ R@ R@ R@ R@ R@j      
		FFFFFFFF******mfmoo..IJM+FuUU      	 	 	DD	s   &AB/ /B87B8