From 69cfe44adef720adf343ac704deae3d033e7e636 Mon Sep 17 00:00:00 2001 From: Nikhil Vangumalla Date: Thu, 3 Dec 2020 22:03:12 +0530 Subject: [PATCH] testcases for customer_refund and fee_refund, removed query in customer_refund --- src/rush/payments.py | 48 +++---------- src/test/test_current.py | 151 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 39 deletions(-) diff --git a/src/rush/payments.py b/src/rush/payments.py index 75ff8f5a..78794339 100644 --- a/src/rush/payments.py +++ b/src/rush/payments.py @@ -137,7 +137,7 @@ def payment_received_event( payment_received_amt = Decimal(event.amount) payment_type = event.payment_type - if not user_loan or payment_type == "card_reload_fees": + if not user_loan or payment_type in ("card_reload_fee", "card_activation_fee"): assert user_product_id is not None if payment_type == "downpayment": @@ -423,39 +423,6 @@ def customer_refund( payment_request_id: str, ): - payment_request_data = ( - session.query(PaymentRequestsData) - .filter( - PaymentRequestsData.user_id == user_loan.user_id, - PaymentRequestsData.payment_request_id == payment_request_id, - PaymentRequestsData.payment_request_status == "Paid", - PaymentRequestsData.row_status == "active", - ) - .one() - ) - - fee = ( - session.query(Fee) - .filter( - Fee.name == payment_request_data.type, - Fee.gross_amount == payment_request_data.payment_request_amount, - ) - .order_by(Fee.updated_at.desc()) - .first() - ) - - if fee is None: - return {"result": "error", "message": "No fee found"} - - fee_refund( - session=session, - user_loan=user_loan, - payment_amount=payment_amount, - payment_date=payment_date, - payment_request_id=payment_request_id, - fee=fee, - ) - lt = LedgerTriggerEvent( name="customer_refund", loan_id=user_loan.loan_id, @@ -468,16 +435,17 @@ def customer_refund( session.add(lt) session.flush() - customer_refund_event( + create_ledger_entry_from_str( session=session, - loan_id=user_loan.loan_id, - lender_id=user_loan.lender_id, - event=lt, + event_id=lt.id, + debit_book_str=f"{user_loan.loan_id}/loan/pre_payment/l", + credit_book_str=f"{user_loan.lender_id}/lender/pg_account/a", + amount=payment_amount, ) update_journal_entry(user_loan=user_loan, event=lt) - return {"result": "success", "message": "Customer Refund successfull"} + return {"result": "success", "message": "Prepayment Refund successfull"} def fee_refund( @@ -508,3 +476,5 @@ def fee_refund( ) update_journal_entry(user_loan=user_loan, event=lt) + + return {"result": "success", "message": "Fee Refund successfull"} diff --git a/src/test/test_current.py b/src/test/test_current.py index 8f9c2110..7635b488 100644 --- a/src/test/test_current.py +++ b/src/test/test_current.py @@ -70,6 +70,8 @@ UserPy, ) from rush.payments import ( + customer_refund, + fee_refund, payment_received, refund_payment, ) @@ -3480,3 +3482,152 @@ def test_readjust_future_payment_with_extension(session: Session) -> None: assert emis[1].payment_status == "Paid" assert emis[2].payment_status == "UnPaid" assert emis[2].payment_received == Decimal("34.00") + + +def test_customer_fee_refund(session: Session) -> None: + test_lenders(session) + card_db_updates(session) + user = User( + id=99, + performed_by=123, + ) + session.add(user) + session.flush() + + user_loan = create_user_product( + session=session, + user_id=user.id, + card_activation_date=parse_date("2020-11-02").date(), + card_type="ruby", + rc_rate_of_interest_monthly=Decimal(3), + lender_id=62311, + ) + + create_card_swipe( + session=session, + user_loan=user_loan, + txn_time=parse_date("2020-11-04 19:23:11"), + amount=Decimal(1000), + description="BigB.com", + txn_ref_no="dummy_txn_ref_no", + trace_no="123456", + ) + + bill = bill_generate(user_loan=user_loan) + + latest_bill = user_loan.get_latest_bill() + assert latest_bill is not None + + bill = accrue_late_charges(session, user_loan, parse_date("2020-11-15"), Decimal(118)) + + fee_due = ( + session.query(BillFee) + .filter(BillFee.identifier_id == bill.id, BillFee.name == "late_fee") + .one_or_none() + ) + assert fee_due.net_amount == Decimal(100) + assert fee_due.gross_amount == Decimal(118) + + payment_received( + session=session, + user_loan=user_loan, + payment_amount=Decimal(1100), + payment_date=parse_date("2020-11-15"), + payment_request_id="s33234", + ) + + bill_fee = session.query(Fee).filter_by(id=fee_due.id).one_or_none() + + assert bill_fee.fee_status == "PAID" + assert bill_fee.net_amount_paid == Decimal(100) + assert bill_fee.igst_paid == Decimal(18) + assert bill_fee.gross_amount_paid == Decimal(118) + + status = fee_refund( + session=session, + user_loan=user_loan, + payment_amount=Decimal("100"), + payment_date=parse_date("2020-11-16"), + payment_request_id="s33234", + fee=bill_fee, + ) + assert status["result"] == "success" + + fee = session.query(Fee).filter_by(id=fee_due.id).one_or_none() + + assert fee.fee_status == "REFUND" + + +def test_customer_prepayment_refund(session: Session) -> None: + test_lenders(session) + card_db_updates(session) + user = User( + id=99, + performed_by=123, + ) + session.add(user) + session.flush() + + user_loan = create_user_product( + session=session, + user_id=user.id, + card_activation_date=parse_date("2020-11-02").date(), + card_type="ruby", + rc_rate_of_interest_monthly=Decimal(3), + lender_id=62311, + ) + + create_card_swipe( + session=session, + user_loan=user_loan, + txn_time=parse_date("2020-11-04 19:23:11"), + amount=Decimal(1000), + description="BigB.com", + txn_ref_no="dummy_txn_ref_no", + trace_no="123456", + ) + + bill_generate(user_loan=user_loan) + + latest_bill = user_loan.get_latest_bill() + assert latest_bill is not None + + payment_received( + session=session, + user_loan=user_loan, + payment_amount=Decimal(5000), + payment_date=parse_date("2020-11-13"), + payment_request_id="a12318", + ) + + _, prepayment_amount = get_account_balance_from_str( + session, book_string=f"{user_loan.loan_id}/loan/pre_payment/l" + ) + + assert prepayment_amount == Decimal(4000) + + customer_refund( + session=session, + user_loan=user_loan, + payment_amount=Decimal(3000), + payment_date=parse_date("2020-11-16"), + payment_request_id="a12318", + ) + + _, prepayment_amount = get_account_balance_from_str( + session, book_string=f"{user_loan.loan_id}/loan/pre_payment/l" + ) + assert prepayment_amount == Decimal(1000) + + customer_refund( + session=session, + user_loan=user_loan, + payment_amount=Decimal(1000), + payment_date=parse_date("2020-11-22"), + payment_request_id="a12318", + ) + + _, prepayment_amount = get_account_balance_from_str( + session, book_string=f"{user_loan.loan_id}/loan/pre_payment/l" + ) + assert prepayment_amount == Decimal(0)