2
2
from decimal import Decimal
3
3
4
4
from django .contrib .auth .decorators import login_required
5
- from django .db .models import Prefetch
5
+ from django .db .models import Prefetch , Sum , Q , DecimalField
6
+ from django .db .models .functions import Coalesce
6
7
from django .http import JsonResponse
7
8
from django .shortcuts import render
8
9
@@ -31,8 +32,22 @@ def loan_dashboard(request):
31
32
32
33
33
34
class LoanAPI ():
35
+
34
36
@login_required ()
35
- def add_loan_api (request ):
37
+ def list_loan (request ):
38
+ """
39
+ This function returns JSON data for the loan entry
40
+ """
41
+ all_loan = Loan .objects .prefetch_related (
42
+ Prefetch ('transaction_set' ,
43
+ queryset = Transaction .objects .all ().order_by ('transaction_date' ))
44
+ ).filter (tenant_id = request .user .id ).distinct ().order_by ('-lending_date' )
45
+ return JsonResponse ({'status' : 'success' ,
46
+ 'all_loans' : LoanSerializer (instance = all_loan , many = True ).data ,
47
+ })
48
+
49
+ @login_required ()
50
+ def add_loan (request ):
36
51
"""
37
52
This function records new loan entry
38
53
"""
@@ -57,16 +72,38 @@ def add_loan_api(request):
57
72
})
58
73
59
74
@login_required ()
60
- def add_loan_transaction_api (request ):
75
+ def add_transaction (request ):
61
76
"""
62
77
This function records new transaction entry
63
78
"""
64
79
if request .method == 'POST' :
65
80
form = TransactionForm (request .POST )
66
81
if form .is_valid ():
82
+ # Fetch loan data with principal sum using annotations
83
+ loan_data = Loan .objects .filter (tenant_id = request .user .id ,
84
+ id = form .data ['loan_id' ]).annotate (
85
+ principal_sum = Coalesce (Sum ('transaction__transaction_amount' ,
86
+ filter = Q (transaction__type = 'PRINCIPAL' )), 0 ,
87
+ output_field = DecimalField ())
88
+ ).values ('amount' , 'principal_sum' ).first ()
89
+ if not loan_data :
90
+ return JsonResponse ({'status' : 'false' , 'error' : 'Loan not found.' })
91
+
92
+ transaction_amount = Decimal (form .data ['transaction_amount' ])
93
+ if form .data ['type' ] == 'PRINCIPAL' :
94
+ total_principal = loan_data ['principal_sum' ] + transaction_amount
95
+ if total_principal > loan_data ['amount' ]:
96
+ return JsonResponse ({
97
+ 'status' : 'false' ,
98
+ 'error' : f'Cannot accept ₹{ transaction_amount } principal transaction. '
99
+ f'It will exceed the principal amount of ₹{ loan_data ["amount" ]} ' ,
100
+ })
101
+
102
+ if total_principal == loan_data ['amount' ]:
103
+ Loan .objects .filter (id = form .data ['loan_id' ]).update (status = False )
104
+
105
+ # Save the transaction
67
106
saved_transaction = form .save ()
68
- # todo; Update Loan Status to 0, when all the added transactions add up to loan_amount
69
- # todo; Validate Principal amount not exceeding loan amount
70
107
return JsonResponse ({'status' : 'success' ,
71
108
'saved_transaction' : TransactionSerializer (
72
109
instance = saved_transaction ,
@@ -77,14 +114,17 @@ def add_loan_transaction_api(request):
77
114
})
78
115
79
116
@login_required ()
80
- def get_loan_api (request ):
117
+ def delete_transaction (request , transaction_id ):
81
118
"""
82
- This function returns JSON data for the loan entry
119
+ This functions used to delete transaction
83
120
"""
84
- all_loan = Loan .objects .prefetch_related (
85
- Prefetch ('transaction_set' ,
86
- queryset = Transaction .objects .all ().order_by ('transaction_date' ))
87
- ).filter (tenant_id = request .user .id , status = True ).distinct ().order_by ('-lending_date' )
88
- return JsonResponse ({'status' : 'success' ,
89
- 'all_loans' : LoanSerializer (instance = all_loan , many = True ).data ,
90
- })
121
+ try :
122
+ Transaction .objects .get (id = transaction_id ,
123
+ loan_id__tenant_id = request .user .id ).delete ()
124
+ return JsonResponse ({'status' : 'success' ,
125
+ 'deleted' : transaction_id ,
126
+ })
127
+ except Transaction .DoesNotExist :
128
+ return JsonResponse ({'status' : 'false' ,
129
+ 'error' : 'Transaction does not exist' ,
130
+ })
0 commit comments