
    )iV                        d Z ddlZddl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mZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ dd	lmZmZ dd
l m!Z!  G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d de      Z& G d de      Z' G d de      Z( G d de      Z) G d de      Z* G d de"      Z+ded e,d!efd"Z-d#e)d$e.d!e.fd%Z/d(d&e0d!e.fd'Z1y))z"
Payment Request request schemas.
    N)ListOptional)datetimetimezone)relativedelta)	BaseModelFieldfield_validatormodel_validator)regexp)PaymentCurrenciesPaymentFrequenciesPaymentAuthorizationTypesSurchargeTypesTaxTypePaymentRequestStatusTypesSplitPaymentTypesDiscountTypesRecurringPaymentEndTypesRecurringPaymentRepeatTypesRecurringPaymentIntervals)
BaseSchemaAddressCreateRequestSchema)MAX_UPLOAD_LIMITc                      e Zd ZU dZ edd      Zee   ed<    edd      Z	ee
   ed<    edd	      Zee
   ed
<    edd      Zee
   ed<    edd      Zee
   ed<    edd      Zee
   ed<    edd      Zee
   ed<    edd      Zee
   ed<    edd      Zee
   ed<    eej&                  d      Zee   ed<    edd      Zee
   ed<    edd      Zee   ed<    edd      Zee   ed<    ed d!      Zee   ed"<   y)#PaymentRequestBasezBase payment request schemaNz Reference number for the paymentdefaultdescription	referenceFz"Save payment method for future usesave_payment_methodzAllow tips on this payment	allow_tipzRequire billing addressrequire_billing_addresszRequire SMS authorizationrequire_sms_authorizationzRequire shipping addressrequire_shipping_addresszRequire signature authorizationrequire_signature_authorizationzRequire CVV verificationrequire_cvvzIs payment certifiedis_certifiedzTax type for the paymenttax_typezConfigure payment adjustmentconfigure_adjustment zMessage for the payment requestmessagezDescription of payment requestr   r   zTax percentagetax_percent)__name__
__module____qualname____doc__r	   r    r   str__annotations__r!   boolr"   r#   r$   r%   r&   r'   r(   r   NOTAXr)   r*   r,   r   r-   float     h/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/src/apps/payment_requests/schemas/requests.pyr   r      sD   %$T?abIx}b*/Ko*p$p %eA] ^Ix~^.3EOh.iXd^i05eQl0mx~m/4UPj/khtnk6;EWx6y#Xd^y"'C]"^K$^#(DZ#[L(4.["'Ke"fHhwf+0Lj+k(4.k"2;\]GXc]]!&tAa!bK#b#(@P#QK%Qr8   r   c                   J    e Zd ZU dZ ed      Zeed<    ed      Ze	ed<   y)PaymentSplitTenderSchemazPayment split tender schemazPayment method for split tenderr   payment_methodzAmount for this splitamountN)
r.   r/   r0   r1   r	   r=   r2   r3   r>   r6   r7   r8   r9   r;   r;   /   s%    %,MNNCN&=>FE>r8   r;   c                   f    e Zd ZU dZ ed      Zeed<    ed      Ze	ed<    ed      Z
eed<   y	)
'PartialPaymentRequestConfigCreateSchemaz$Partial payment configuration schemazType of partial paymentr<   partial_payment_typezValue for partial paymentpartial_payment_valuezDue date for partial paymentdue_dateN)r.   r/   r0   r1   r	   rA   r   r3   rB   r6   rC   r   r7   r8   r9   r@   r@   5   s8    ..3@Y.Z+Z#(5P#Q5Q+IJHhJr8   r@   c                   R    e Zd ZU dZ ed      Zeed<    edd      Ze	e
   ed<   y	)
PaymentRequestMethodSchemazPayment request method schemazPayment method typer<   r=   FzIs default payment methodr   
is_defaultN)r.   r/   r0   r1   r	   r=   r2   r3   rF   r   r4   r7   r8   r9   rE   rE   <   s+    ',ABNCB!&uB]!^J^r8   rE   c                       e Zd ZU dZ edd      Zee   ed<    ed      Z	ee
   ed<    ed	d
      Zee   ed<    edd      Zee   ed<    ed	d      Zee   ed<   y)PaymentRequestAdjustmentsCreatez"Payment request adjustments schemaFzHas discountr   is_discountedType of discountr<   discount_typer   Discount amountdiscount_amountzHas surchargeis_surchargedzSurcharge amountsurcharge_amountN)r.   r/   r0   r1   r	   rI   r   r4   r3   rK   r   rM   r6   rN   rO   r7   r8   r9   rH   rH   B   sq    ,$)%^$TM8D>T-2?Q-RM8M*R',QDU'VOXe_V$)%_$UM8D>U(-aEW(XhuoXr8   rH   c                      e Zd ZU dZ ed      Zeed<    edd      Ze	e   ed<    ed	      Z
eed
<    ed      Zeed<    edd      Ze	e   ed<    edd      Ze	e   ed<    edd      Ze	e   ed<    edd      Ze	e   ed<    edd      Ze	e   ed<    edd      Ze	e   ed<    ed      Ze	e   ed<    edd      Ze	e   ed<   y)PaymentRequestLineItemCreatez Payment request line item schemazTitle of line itemr<   titler+   zDescription of line itemr   r   zUnit price of item
unit_pricezQuantity of itemsquantityr   zTax for itemtaxNzTax ID referencetax_idzProduct ID reference
product_idzCost of itemcostzUpcharge amountupchargerL   discountrJ   rK   zCalculated tax amount
tax_amount)r.   r/   r0   r1   r	   rR   r2   r3   r   r   rS   r6   rT   intrU   rV   rW   rX   rY   rZ   rK   r   r[   r7   r8   r9   rQ   rQ   K   s    *#78E38!&r?Y!ZK#Z*>?J?&9:Hc: GC%G!$<NOFHSMO %d@V WJW!!HD(5/H %a=N OHhuoO %a=N OHhuoO-2?Q-RM8M*R"'?V"WJWr8   rQ   c                       e Zd ZU dZ edd      Zee   ed<    edd      Z	ee
   ed<    ed	
      Zeed<    edd      Zee
   ed<    edd      Zee   ed<   y)PaymentRequestProductCreatezPayment request product schemaNzProduct slugr   product_slugr   zUnit price overriderS   zQuantity of productsr<   rT   zTax percentage overrider-   FzIs product taxable
is_taxable)r.   r/   r0   r1   r	   r_   r   r2   r3   rS   r6   rT   r\   r-   r`   r4   r7   r8   r9   r^   r^   [   sl    ("'."QL(3-Q"'?T"UJU&<=Hc=#(C\#]K%]!&uBV!WJWr8   r^   c                       e Zd ZU dZ ed      Zeed<    ed      Ze	ed<    ed      Z
eed<    ed	      Zeed
<    ed      Zeed<   y)SplitPaymentRequestCreatez*Split payment request configuration schemazSequence number of splitr<   sequencez!Type of split (percentage/amount)
split_typezValue of splitsplit_valuezBilling date for splitbilling_datezDue date for splitrC   N)r.   r/   r0   r1   r	   rc   r\   r3   rd   r   re   r6   rf   r   rC   r7   r8   r9   rb   rb   d   sV    4&@AHcA$)6Y$ZJ!Z+;<K<"/GHL(H+?@Hh@r8   rb   c                   6   e Zd ZU dZ edd      Zee   ed<    edd      Z	ee
   ed<    ed	
      Ze
ed<    ed
      Zeed<    ed
      Zeed<    edd      Zee   ed<    ed
      Zeed<    edd      Zee
   ed<    edd      Zee   ed<   y)RecurringPaymentRequestCreatez.Recurring payment request configuration schemaFzProrate first paymentr   prorate_first_paymentNzProrate dateprorate_datez!Start date for recurring paymentsr<   
start_datezRepeat typerepeat_typezInterval typeinterval   zInterval valueinterval_valuezEnd typeend_typezEnd dateend_datezPay until countpay_until_count)r.   r/   r0   r1   r	   ri   r   r4   r3   rj   r   rk   rl   r   rm   r   ro   r\   rp   r   rq   rr   r7   r8   r9   rh   rh   m   s    8,1%Md,e8D>e',T~'VL(8$V -PQJQ/4/OK,O*/O*LH'L$)!AQ$RNHSMR).:)FH&F#(:#NHhx N%*4EV%WOXc]Wr8   rh   c                      e Zd ZU dZ edd      Zeed<    ed      Ze	ed<    ed	      Z
eed
<    ed      Zeed<    ed      Zeed<    ed      Zeed<    ed      Zee   ed<    edd      Zee   ed<    edd      Zeee      ed<    edd      Zee   ed<    edd      Zeee      ed<    edd      Zee   ed<    edd      Zee   ed<    edd       Zee    ed!<    edd"      Z!ee"   ed#<    edd$      Z#ee    ed%<    edd&      Z$eee%      ed'<    edd(      Z&eee'      ed)<    edd*      Z(eee)      ed+<    edd,      Z*ee+   ed-<    edd.      Z,eee"      ed/<    ed0d1d23      Z-ee	   ed4<    edd5      Z.ee   ed6<    edd7      Z/ee   ed8<    edd9      Z0ee1   ed:<    edd;      Z2ee   ed<<    edd=      Z3ee   ed><    edd?      Z4ee   ed@<    eddA      Z5ee   edB<    eddC      Z6ee   edD<    eddE      Z7ee   edF<    eddG      Z8ee   edH<    edd5      Z9ee   edI<    edd7      Z:ee   edJ<    eddK      Z;ee   edL<    eddM      Z<ee=   edN<    eddO      Z>ee   edP<    e?d!      e@dQ               ZA e?d+      e@dR               ZB e?d-      e@dS               ZC e?d'      e@dT               ZD e?d/      e@dU               ZE eFdVW      e@dX               ZG eFdVW      e@dY               ZH eFdVW      e@dZ               ZI eFdVW      e@d[               ZJ eFdVW      e@d\               ZK eFdVW      e@d]               ZL G d^ d_      ZMy)`"MerchantPaymentRequestCreateSchemaz-Schema for creating merchant payment requestsFz#Save this payment request as draft?r   is_draftz0Amount of Payment Request, accepts decimal inputr<   r>   z@Three-letter ISO currency code, in lowercase. Eg: usd, eur, etc.currencyzNUnique ID (cus_xxx) of customer for whom this payment request is to be createdcustomer_idz:ID of payer for whom this payment request is to be createdpayer_idz4Frequency of payment, (Split / Recurring / One-time)payment_frequencyzKPaymentRequest authorisation type, (Pre-Authorized / Request-Authorization)authorization_typeNzRedirection url of the paymentpayment_redirect_urlz/Split Tender details of the single type Paymentpayment_split_tendersz/Partial payment details of the one time paymentpartial_paymentzPayment Methods Requestspayment_methodsz?Payment Adjustment Details which consist of surcharge/Discountspayment_adjustmentszCFrequency of payment split, (Weekly / Monthly / Quarterly / Yearly)payment_split_frequencyz.Due date and time of payment as unix timestamprC   z#Shipping Address id for the paymentshipping_address_idz2Billing date and time of payment as unix timestamprf   z7Any transactional items included in the payment request
line_itemsz'Billed products in this payment requestbilled_productsz'Configuration for split payment requestsplit_configz+Configuration for recurring payment requestrecurring_configz)Uploaded file ids of optional attachmentsattachmentsr   i z%Shipping fees for the billable items.)geler   shipping_feez4Enable Email notifications for this payment request?enable_emailz2Enable SMS notifications for this payment request?
enable_smsz;Billing address that will be attached to the payment methodbilling_addressz=ID of approver for whom this payment request is to be createdapprover_idz/Receive request email for the payment for payerpayer_email_request_enabledz/Receive receipt email for the payment for payerpayer_email_receipt_enabledz-Receive request sms for the payment for payerpayer_sms_request_enabledz-Receive receipt sms for the payment for payerpayer_sms_receipt_enabledz2Receive receipt email for the payment for approverapprover_email_receipt_enabledz0Receive receipt sms for the payment for approverapprover_sms_receipt_enabledenable_email_receiptenable_sms_receiptz%Surcharge enabled for cost adjustmentis_surcharge_enabledz"Surcharge type inclusive/exclusivesurcharge_typez$Terms and conditions for the invoiceinvoice_termsc                 "    |t        |d      S |S )zValidate due daterC   )validate_payment_due_dateclsvs     r9   validate_due_datez4MerchantPaymentRequestCreateSchema.validate_due_date   s     =,Q
;;r8   c                 2   g }|jt        |      dk  rt        d      |D ]9  }	 t        |j                  d      |_        t        |j                  d      |_        ; t        |      dkD  r|d   |S # t        $ r}|j                  |       Y d}~ 5d}~ww xY w)zValidate split configurationN   zAt least 2 splits are requiredzsplit_config.due_datezsplit_config.billing_dater   )len
ValueErrorr   rC   rf   append)r   r   errorssplites        r9   validate_split_configz8MerchantPaymentRequestCreateSchema.validate_split_config   s     $&=1vz !ABB %>u~~Of%gEN)B5CUCUWr)sE& 6{QQi " MM!$s   6A11	B:BBc                 \   |t        |j                  d      |_        |j                  rt        |j                  d      |_        |j                  r[t        |j                  d      |_        |j                  j                         |j                  j                         k  rt        d      |S )z Validate recurring configurationzrecurring_config.start_datezrecurring_config.prorate_datezrecurring_config.end_datezJrecurring_config.end_date must be greater than recurring_config.start_date)r   rk   ri   rj   rq   dater   r   s     r9   validate_recurring_configz<MerchantPaymentRequestCreateSchema.validate_recurring_config   s     =4Q\\C`aAL&&!:1>>Kj!kzz6qzzC^_
::??$(9(9(;;$%qrrr8   c                     |S )zValidate line itemsr7   r   s     r9   validate_line_itemsz6MerchantPaymentRequestCreateSchema.validate_line_items   s	     r8   c                 T    |%t        |      t        kD  rt        dt         d      |S )zValidate attachmentszA maximum of z attachments can be included)r   r   r   r   s     r9   validate_attachmentsz7MerchantPaymentRequestCreateSchema.validate_attachments   s4     =1v(( =1A0BB^!_`` r8   before)modec                 R   |j                  dd      }|t        j                  k(  r|j                  dd      st        d      |S |t        j                  k(  r|j                  dd      st        d      |S |t        j
                  k(  r|j                  dd      st        d	      |S )
z1Validate payment configuration based on frequencyry   NrC   Fzdue_date is requiredr   zsplit_config is requiredr   zrecurring_config is required)getr   ONE_TIMEr   SPLIT	RECURRING)r   valuesry   s      r9   validate_payment_configz:MerchantPaymentRequestCreateSchema.validate_payment_config   s     #JJ':DA 2 ; ;;::j%0 !788  "4":"::::ne4 !;<<  "4">">>::0%8 !?@@r8   c                 h    |j                  dd      }|s|j                  dd      t        d      |S )zValidate authorization typeru   Frz   Nz$Please provide an Authorization Type)r   r   )r   r   ru   s      r9   validate_authorization_typez>MerchantPaymentRequestCreateSchema.validate_authorization_type   s9     ::j%0FJJ';TBJCDDr8   c                     |j                  dd      }|j                  d      }|s0|t        j                  k(  r|j                  dd      st        d      |S )zValidate notification settingsru   Frz   r   zDPlease enable email notifications for request authorization payments)r   r   REQUEST_AUTHr   )r   r   ru   	auth_modes       r9   validate_notificationsz9MerchantPaymentRequestCreateSchema.validate_notifications   sT     ::j%0JJ34	5BBBzz.%8$%kllr8   c                    |j                  dd      }|j                  dd      }g }|t        j                  k(  rO|j                  dg       }|D ]$  }|d   t        j                  k(  rH|d   dk  r|j                  t        d	              n|d   |k\  rz|j                  t        d
              n|d   t        j                  k(  rH|d   dk  r|j                  t        d              n|d   dkD  r|j                  t        d              nht        t        di ||      }|dk  r#|j                  t        d|d    d              n+|dkD  s|j                  t        d|d    d              n t        |      dkD  r|d   |t        j                  k(  r	 |t        j                  fvr |dk  rt        d	      |dkD  rt        d      |S )zValidate transaction amountsr>   r   ry   Nr   rd   re   d   zAmount must be greater than $1z)Amount must be lesser than payment amountrn   z3Split Percentage must be greater than or equal to 1c   z3Split Percentage must be lesser than or equal to 99zSplit amount for sequence rc   z must be greater than $1iz must be lesser than $1000000z#Amount must be lesser than $1000000r7   )r   r   r   r   AMOUNTr   r   
PERCENTAGEcalculate_split_amountrb   r   r   )r   r   transaction_amountry   r   splitss	split_amts           r9   validate_transaction_amountz>MerchantPaymentRequestCreateSchema.validate_transaction_amount  s    $ZZ!4"JJ':DA#% 2 8 88ZZ3F \?&7&>&>>'#-j1Q&RS=)-??j1\&]^|_(9(D(DD'!+j1f&gh'",j1f&gh23L3Qq3QSef	s?MM*/I!J-Xp-q"rsx'MM*/I!J-Xu-v"wx-. 6{QQi"4">">>%7%=%=$>>!C' !ABB#h. !FGGr8   c                     |j                  dg       }t        |j                  dd            }|j                  dd      }d}t        |      dkD  r	 |S )z-Validate product amounts and calculate totalsr   r   r   ry   N)r   round_half_upr   )r   r   r   r   ry   transaction_amts         r9   validate_products_amountz;MerchantPaymentRequestCreateSchema.validate_products_amount2  sX     !**%6;$VZZ%BC"JJ':DA!"!# r8   c                 l    |j                  dg       }t        |      dk(  rt        j                  |d<   |S )zValidate tax typer   r   r)   )r   r   r   r5   )r   val	line_items      r9   validate_tax_typez4MerchantPaymentRequestCreateSchema.validate_tax_typeA  s2     GGL"-	y>Q%mmC
O
r8   c                   h    e Zd ZdZdddddddd	d
dddddgddddddddddddgdddddddddiZy))MerchantPaymentRequestCreateSchema.ConfigzSchema configurationexamplecus_asbbeqwb123123r   request_authcardTpm_asbbeqwb123123g      Y@usdHorlickszTaste for goodr   rn   )rR   r   rS   rT   
percentage2   z 2021-10-18T12:33:28.146250+00:00)rc   rd   re   rf   rC   r   Fz 2022-01-19T12:33:28.146250+00:00on_datemonth   r   z 2023-01-11T12:33:28.146250+00:00)ri   rk   rl   rm   ro   rp   rq   )rw   ry   rz   r=   r!   payment_method_tokenr>   rv   r   r   r   N)r.   r/   r0   r1   json_schema_extrar7   r8   r9   Configr   J  s    "3%,&4"('+(; ! ",'7&)$%	 %&&2')(J$F %&&2')(J$F!" .3"D#, '&( & B%C*,
r8   r   )Nr.   r/   r0   r1   r	   ru   r4   r3   r>   r6   rv   r   rw   r2   rx   ry   r   rz   r   r   r{   r|   r   r;   r}   r@   r~   rE   r   rH   r   rC   r   r   r\   rf   r   rQ   r   r^   r   rb   r   rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r7   r8   r9   rt   rt   z   s8   756[\Hd\&XYFEY"'4v"wHw)yzKz&bcHcc,1>t,u)u>C  Q^  ?_!:;  _*/Jj*k(3-kFKD  ^O  GP8D)A$BC  PINt  aR  JSOXEF  SBGYsBtOXd#=>?tEJ4  ]^  F_"AB  _-24  EJ  .KXc]  K#(:j#kHhx k).t@e)f#f',T>r'sL(8$s?DT  WP  @QJ:;<  QCH  [D  DEOXd#>?@  E>CDU~>L(4 9:;@Ed  XE  AFh<=  F',T>i'jK$s)$j$)Q5Fm$nL(5/n#(Cy#zL(4.z!&uAu!vJv<A$  TQ  =ROX89  R!&t9x!yK#y27Iz2{${27Iz2{${05dGv0wx~w05dGv0wx~w5:4  MA  6B"HTN  B38J|3} (4.}+0Bx+y(4.y).t@t)uu+0Bi+j(4.j/4TFj/kNH^,k#(:`#aM8C=aZ   ! ^$  %" '(
  )
 \"  # ]#  $ (#  $ (#  $ (#  $ (#+  $+Z (#  $ (#  $.
 .
r8   rt   r   fieldreturnc                    t        j                  t        j                        j	                  dddd      }t        j                  t        j                        }	 t        j
                  | j                         t        j                        }||k  r.d}|t        t        |            z
  }||k  rt        | d	      |S # t        $ r}t        d| d      d}~ww xY w)
a   
    Validate payment due date
    
    Args:
        val: The datetime to validate
        field: The field name for error messages
        
    Returns:
        datetime: Validated datetime
        
    Raises:
        ValueError: If date is invalid
    r   )hourminutesecondmicrosecond)tzzPlease provide a valid z for the paymentN   )hoursz. must be greater than or equal to today's date)r   nowr   utcreplacefromtimestamp	timestamp	Exceptionr   r   r\   )r   r   	utc_todaypayment_due_dater   max_utc_offsetmax_utc_offset_dts          r9   r   r   {  s     #,,x||4<<q = I "*hll!;L#11MMO
 )#%S=P<P(QQ//w&TUVV  L25'9IJKKLs   3C 	C CC r   total_amountc                     | j                   t        j                  k(  rt        | j                  dz  |z        S | j                   t        j
                  k(  rt        | j                        S y)z
    Calculate split amount based on type and value
    
    Args:
        split: Split configuration
        total_amount: Total payment amount
        
    Returns:
        float: Calculated split amount
    r   g        )rd   r   r   r   re   r   )r   r   s     r9   r   r     s]     ,777e//#5EFF			.55	5U..//r8   decimalsc                 `    t        | d|z  z  dz         d|z  z  }|dz  dk(  rt        |      }|S )z
    This function rounds a number half up. Eg. 18.5 will round to 19
    
    Args:
        number: Number to round
        decimals: Number of decimal places
        
    Returns:
        float: Rounded number
    
   g      ?rn   r   )r\   )numberr   rounded_values      r9   r   r     sB     "h,/#56"h,GMqAM*r8   )r   )2r1   base64pytztypingr   r   r   r   dateutil.relativedeltar   pydanticr   r	   r
   r   src.apps.base.utilsr   src.apps.payment_requests.enumsr   r   r   r   r   r   r   r   r   r   r   src.apps.base.schemas.commonr   r   src.core.utils.constantsr   r   r;   r@   rE   rH   rQ   r^   rb   rh   rt   r2   r   r6   r   r\   r   r7   r8   r9   <module>r     s     ! ' 0 G G &    P 5R R$?z ?Kj K_ _Yj YX: X X* XA
 A
XJ 
X~
); ~
B8 C H D"; 5 UZ $C  r8   