예제 #1
0
def send_frequent_invitations():
    invite_contacts = Contacts.objects.filter(is_member=False, is_valid=True, invitation_sent=False)
    contacts_to_sent = invite_contacts.order_by('phone_number').values_list('phone_number', flat=True).distinct()
    contacts_to_send = contacts_to_sent[:500]
    numbers = ','.join(contacts_to_send)
    numbers = numbers.encode("ascii")
    sms = sms_utils.Sms()
    message = "Opencircles is a platform where friends, family and colleagues can pull resources to save and invest" \
              " in consumer credit. Create a circle and get your whatsapp group to join and save in the circle and" \
              " stand a chance to win Kes 5,000 every friday this festive season." \
              " Get the app on play store at https://goo.gl/5KWXhx"
    if invite_contacts.exists():
        # response,unsent = sms.sendmultiplesms(numbers, message)
        # if response:
        #     invite_contacts.exclude(phone_number__in=unsent).update(invitation_sent=True)
        pass
    else:
        print("No contacts in this table")

    loans = loan_utils.Loan()
    #send loan reminders from this function
    loans.loan_repayment_reminder()

    #Delete loans that have expired I.E have exceeded the 1 week time span without all the guarantors accepting
    loans.delete_expired_loan()

    circle_instance = circle_utils.Circle()
    circle_instance.deactivate_circle_member()
    circle_instance.delete_inactive_circles()
    return "Sent messages successfully"
예제 #2
0
def unlocking_guarantors_shares(guarantors_id, shares_desc):
    instance = loan_utils.Loan()
    guarantors = GuarantorRequest.objects.filter(id__in=guarantors_id)
    instance.unlock_guarantors_shares(guarantors, shares_desc)
    message = "unlocked shares successfully"
    with open('celery_unlock_guarantors_shares_worker_file.txt',
              'a') as post_file:
        post_file.write(str(guarantors_id))
        post_file.write("\n")
    return message
예제 #3
0
def sending_guarantee_requests(guarantors_id, member_id):
    guarantors = GuarantorRequest.objects.filter(id__in=guarantors_id)
    member = Member.objects.get(id=member_id)
    instance = loan_utils.Loan()
    instance.send_guarantee_requests(guarantors, member)
    log_message = "Sent guarantee request successfully"
    with open('celery_updating_loan_limit_worker_file.txt', 'a') as post_file:
        post_file.write(str(guarantors_id))
        post_file.write("\n")
        post_file.write(str(member_id))
        post_file.write("\n")
    return log_message
예제 #4
0
def updating_loan_limit(circle_id, member_id):
    instance = loan_utils.Loan()
    circle = Circle.objects.get(id=circle_id)
    member = Member.objects.get(id=member_id)
    instance.update_loan_limit(circle, member)
    log_message = "Updated loan limit successfully"
    with open('celery_updating_loan_limit_worker_file.txt', 'a') as post_file:
        post_file.write(str(circle_id))
        post_file.write("\n")
        post_file.write(str(member_id))
        post_file.write("\n")
    return log_message
예제 #5
0
def task_share_loan_interest(loan_id):
    loan = LoanApplication.objects.get(id=loan_id)
    with open('celery_share_loan_interest_loan_worker_file.txt',
              'a') as post_file:
        post_file.write(str(loan_id))
        post_file.write("\n")
        post_file.write(str(type(loan_id)))
    instance = loan_utils.Loan()
    instance.share_loan_interest(loan)
    log_message = "Loan interest successful"
    with open('celery_share_loan_interest_worker_file.txt', 'a') as post_file:
        post_file.write(str(loan_id))
        post_file.write("\n")
    return log_message
예제 #6
0
 def post(self, request, *args, **kwargs):
     serializer = PurchaseSharesSerializer(data=request.data)
     if serializer.is_valid():
         pin, amount = serializer.validated_data[
             'pin'], serializer.validated_data['amount']
         circle_acc_number = serializer.validated_data['circle_acc_number']
         circle, member = Circle.objects.get(
             circle_acc_number=circle_acc_number), request.user.member
         if circle.is_active:
             circle_member = CircleMember.objects.get(circle=circle,
                                                      member=member)
             if circle_member.is_active:
                 if amount < settings.MIN_SUBSEQUENT_SHARES:
                     data = {
                         "status":
                         0,
                         "message":
                         "The allowed minimum purchased shares is "
                         "KES {}".format(settings.MIN_SUBSEQUENT_SHARES)
                     }
                     return Response(data, status=status.HTTP_200_OK)
                 valid, response = shares_utils.Shares(
                 ).validate_purchased_shares(amount, circle, member)
                 if valid:
                     wallet_instance = wallet_utils.Wallet()
                     valid, response = wallet_instance.validate_account(
                         request, pin, amount)
                     created_objects = []
                     shares = None
                     if valid:
                         loan_instance = loan_utils.Loan()
                         try:
                             general_instance = general_utils.General()
                             circle_member = CircleMember.objects.get(
                                 circle=circle, member=member)
                             shares = circle_member.shares.get()
                             wallet = member.wallet
                             wallet_balance = wallet_instance.calculate_wallet_balance(
                                 wallet) - amount
                             transaction_code = general_instance.generate_unique_identifier(
                                 'WTD')
                             wallet_desc = "{} confirmed.You have purchased shares worth {} {} in circle {}." \
                                           "New wallet balance is {} {}.".format(transaction_code, member.currency,
                                                                                 amount, circle.circle_name,
                                                                                 member.currency, wallet_balance)
                             wallet_transaction = Transactions.objects.create(
                                 wallet=wallet,
                                 transaction_type="DEBIT",
                                 transaction_time=datetime.datetime.now(),
                                 transaction_desc=wallet_desc,
                                 transaction_amount=amount,
                                 recipient=circle_acc_number,
                                 transaction_code=transaction_code,
                                 source="wallet")
                             created_objects.append(wallet_transaction)
                             print("wallet transaction")
                             print(wallet_transaction.transaction_amount)
                             transaction_code = general_instance.generate_unique_identifier(
                                 'STD')
                             shares_desc = "{} confirmed.You have purchased shares worth {} {} " \
                                           "in circle {}.".format(transaction_code, member.currency,
                                                                  amount, circle.circle_name)
                             shares_transaction = IntraCircleShareTransaction.objects.create(
                                 shares=shares,
                                 transaction_type="DEPOSIT",
                                 sender=circle_member,
                                 recipient=circle_member,
                                 num_of_shares=amount,
                                 transaction_desc=shares_desc,
                                 transaction_code=transaction_code)
                             created_objects.append(shares_transaction)
                             circle_instance = circle_utils.Circle()
                             available_shares = circle_instance.get_available_circle_member_shares(
                                 circle, member)
                             print("available shares")
                             print(available_shares)
                             fcm_available_shares = circle_instance.get_guarantor_available_shares(
                                 circle, member)
                             wallet_serializer = WalletTransactionsSerializer(
                                 wallet_transaction)
                             shares_serializer = SharesTransactionSerializer(
                                 shares_transaction)
                             loan_limit = loan_instance.calculate_loan_limit(
                                 circle, member)
                             print("loan limit")
                             print(loan_limit)
                             data = {
                                 "status": 1,
                                 "wallet_transaction":
                                 wallet_serializer.data,
                                 "shares_transaction":
                                 shares_serializer.data,
                                 "loan_limit": loan_limit
                             }
                         except Exception as e:
                             print(str(e))
                             general_utils.General().delete_created_objects(
                                 created_objects)
                             data = {
                                 "status": 0,
                                 "message": "Unable to complete transaction"
                             }
                             return Response(data,
                                             status=status.HTTP_200_OK)
                         # unblock task
                         # loan_instance.update_loan_limit(circle,member)
                         updating_loan_limit.delay(circle.id, member.id)
                         fcm_instance = fcm_utils.Fcm()
                         fcm_data = {
                             "request_type": "UPDATE_AVAILABLE_SHARES",
                             "circle_acc_number": circle.circle_acc_number,
                             "phone_number": member.phone_number,
                             "available_shares": fcm_available_shares
                         }
                         registration_id = fcm_instance.get_circle_members_token(
                             circle, member)
                         fcm_instance.data_push("multiple", registration_id,
                                                fcm_data)
                         print(fcm_data)
                         return Response(data, status=status.HTTP_200_OK)
                     data = {"status": 0, "message": response}
                     return Response(data, status=status.HTTP_200_OK)
             data = {
                 "status":
                 0,
                 "message":
                 "Unable to deposit to circle.Your account is currently deactivated due"
                 " to delayed loan repayment."
             }
             return Response(data, status=status.HTTP_200_OK)
         data = {
             "status": 0,
             "message": "Unable to deposit to circle.Circle is inactive."
         }
         return Response(data, status=status.HTTP_200_OK)
         data = {"status": 0, "message": response}
         return Response(data, status=status.HTTP_200_OK)
     data = {"status": 0, "message": serializer.errors}
     return Response(data, status=status.HTTP_200_OK)
예제 #7
0
 def post(self, request):
     serializer = SharesWithdrawalSerializer(data=request.data)
     if serializer.is_valid():
         pin = serializer.validated_data['pin']
         circle = Circle.objects.get(circle_acc_number=serializer.
                                     validated_data['circle_acc_number'])
         member = request.user.member
         created_objects = []
         if circle.is_active:
             circle_member = CircleMember.objects.get(circle=circle,
                                                      member=member)
             if circle_member.is_active:
                 if request.user.check_password(pin):
                     amount = serializer.validated_data['amount']
                     valid, response = shares_utils.Shares(
                     ).validate_withdrawal_amount(amount)
                     if valid:
                         circle_instance = circle_utils.Circle()
                         available_shares = circle_instance.get_available_circle_member_shares(
                             circle, member)
                         print("before available_shares")
                         print(available_shares)
                         if amount <= available_shares:
                             shares_tariff = SharesWithdrawalTariff.objects.get(
                                 max_amount__gte=amount,
                                 min_amount__lte=amount)
                             fcm_available_shares = circle_instance.get_guarantor_available_shares(
                                 circle, member)
                             total_amount = amount + shares_tariff.amount
                             if total_amount <= available_shares:
                                 if total_amount <= fcm_available_shares:
                                     shares = None
                                     try:
                                         loan_instance = loan_utils.Loan()
                                         general_instance, wallet_instance = general_utils.General(
                                         ), wallet_utils.Wallet()
                                         circle_member = CircleMember.objects.get(
                                             circle=circle, member=member)
                                         shares = circle_member.shares.get()
                                         time_processed = datetime.datetime.now(
                                         )
                                         transaction_code = general_instance.generate_unique_identifier(
                                             'STW')
                                         shares_desc = "{} confirmed.You have withdrawn shares worth {} {} from " \
                                                       "circle {}.Transaction cost {} {}".format(transaction_code,
                                                                                                 member.currency,
                                                                                                 amount,
                                                                                                 circle.circle_name,
                                                                                                 member.currency,
                                                                                                 shares_tariff.amount)
                                         shares_transaction = IntraCircleShareTransaction.objects.create(
                                             shares=shares,
                                             transaction_type="WITHDRAW",
                                             num_of_shares=total_amount,
                                             transaction_desc=shares_desc,
                                             transaction_code=
                                             transaction_code)
                                         created_objects.append(
                                             shares_transaction)
                                         revenue = RevenueStreams.objects.create(
                                             stream_amount=shares_tariff.
                                             amount,
                                             stream_type="SHARES WITHDRAW",
                                             stream_code=transaction_code,
                                             time_of_transaction=
                                             time_processed)
                                         created_objects.append(revenue)
                                         transaction_code = general_instance.generate_unique_identifier(
                                             'WTC')
                                         wallet_balance = wallet_instance.calculate_wallet_balance(
                                             member.wallet) + amount
                                         wallet_desc = "{} confirmed.You have received {} {} from circle {} shares " \
                                                       "withdrawal.New wallet balance is {} {}".format(transaction_code,
                                                                                                       member.currency,
                                                                                                       amount,
                                                                                                       circle.circle_name,
                                                                                                       member.currency,
                                                                                                       wallet_balance)
                                         wallet_transaction = Transactions.objects.create(
                                             wallet=member.wallet,
                                             transaction_type='CREDIT',
                                             transaction_time=time_processed,
                                             transaction_desc=wallet_desc,
                                             transaction_amount=amount,
                                             transaction_code=
                                             transaction_code,
                                             source="shares")
                                         created_objects.append(
                                             wallet_transaction)
                                         shares_transaction_serializer = SharesTransactionSerializer(
                                             shares_transaction)
                                         wallet_transaction_serializer = WalletTransactionsSerializer(
                                             wallet_transaction)
                                         fcm_available_shares = circle_instance.get_guarantor_available_shares(
                                             circle, member)
                                         print(
                                             "others view of available_shares"
                                         )
                                         print(fcm_available_shares)
                                         available_shares = circle_instance.get_available_circle_member_shares(
                                             circle, member)
                                         print("new available_shares")
                                         print(available_shares)
                                         loan_limit = loan_instance.calculate_loan_limit(
                                             circle, member)
                                         print("loan limit")
                                         print(loan_limit)
                                         data = {
                                             "status":
                                             1,
                                             "shares_transaction":
                                             shares_transaction_serializer.
                                             data,
                                             "wallet_transaction":
                                             wallet_transaction_serializer.
                                             data,
                                             "loan_limit":
                                             loan_limit,
                                             "message":
                                             wallet_desc
                                         }
                                     except Exception as e:
                                         print(str(e))
                                         general_instance = general_utils.General(
                                         )
                                         general_instance.delete_created_objects(
                                             created_objects)
                                         data = {
                                             "status":
                                             0,
                                             "message":
                                             "Unable to process the shares withdrawal request"
                                         }
                                         return Response(
                                             data,
                                             status=status.HTTP_200_OK)
                                     # unblock task
                                     # loan_instance.update_loan_limit(circle,member)
                                     updating_loan_limit.delay(
                                         circle.id, member.id)
                                     fcm_instance = fcm_utils.Fcm()
                                     fcm_data = {
                                         "request_type":
                                         "UPDATE_AVAILABLE_SHARES",
                                         "circle_acc_number":
                                         circle.circle_acc_number,
                                         "phone_number":
                                         member.phone_number,
                                         "available_shares":
                                         fcm_available_shares
                                     }
                                     registration_id = fcm_instance.get_circle_members_token(
                                         circle, member)
                                     fcm_instance.data_push(
                                         "multiple", registration_id,
                                         fcm_data)
                                     return Response(
                                         data, status=status.HTTP_200_OK)
                                 data = {
                                     "status":
                                     0,
                                     "message":
                                     "Unable to perform shares withdrawal request due to"
                                     " pending loan guarantee requests."
                                     "Kindly response to the requests."
                                 }
                                 return Response(data,
                                                 status=status.HTTP_200_OK)
                             data = {
                                 "status":
                                 0,
                                 "message":
                                 "Insufficient shares to cover the shares withdrawal charges"
                             }
                             return Response(data,
                                             status=status.HTTP_200_OK)
                         data = {
                             "status":
                             0,
                             "message":
                             "Amount entered exceeds your available shares."
                         }
                         return Response(data, status=status.HTTP_200_OK)
                     data = {"status": 0, "message": response}
                     return Response(data, status=status.HTTP_200_OK)
                 data = {"status": 0, "message": "Invalid pin"}
                 return Response(data, status=status.HTTP_200_OK)
             data = {
                 "status":
                 0,
                 "message":
                 "Unable to withdraw savings from circle. Your account is currently "
                 "deactivated due to delayed loan repayment."
             }
             return Response(data, status=status.HTTP_200_OK)
         data = {
             "status":
             0,
             "message":
             "Unable to withdraw savings from circle.Circle is inactive."
         }
     data = {"status": 0, "message": serializer.errors}
     return Response(data, status=status.HTTP_200_OK)
예제 #8
0
 def post(self, request):
     serializer = JoinCircleSerializer(data=request.data)
     if serializer.is_valid():
         acc_number, amount, pin = serializer.validated_data[
             'circle_acc_number'], serializer.validated_data[
                 'amount'], serializer.validated_data['pin']
         circle = Circle.objects.get(circle_acc_number=acc_number)
         circle_members_count = CircleMember.objects.filter(
             circle=circle).count()
         has_defaulted = CircleMember.objects.filter(
             member=request.user.member, is_active=False)
         if has_defaulted.exists():
             data = {
                 "status":
                 0,
                 "message":
                 "Unable to join circle.One of your accounts is currently deactivated due"
                 " to delayed loan repayment. Kindly repay your loan to be able to join this circle."
             }
             return Response(data, status=status.HTTP_200_OK)
         if circle_members_count <= settings.MAX_CIRCLE_MEMBER:
             if amount < circle.minimum_share:
                 data = {"status":0, "message":"The allowed minimum initial deposit for circle " \
                                               "{} is KES {}".format(circle.circle_name, circle.minimum_share)}
                 return Response(data, status=status.HTTP_200_OK)
             wallet_instance = wallet_utils.Wallet()
             valid, response = wallet_instance.validate_account(
                 request, pin, amount)
             if valid:
                 created_objects = []
                 try:
                     loan_instance = loan_utils.Loan()
                     general_instance = general_utils.General()
                     member = request.user.member
                     existing_circle_member = CircleMember.objects.filter(
                         member=member).count()
                     wallet_transaction_code = general_instance.generate_unique_identifier(
                         'WTD')
                     shares_transaction_code = general_instance.generate_unique_identifier(
                         'STD')
                     wallet_balance = wallet_instance.calculate_wallet_balance(
                         member.wallet) - amount
                     wallet_desc = "{} confirmed. You have purchased shares worth {} {} in circle {}. " \
                                   "New wallet balance is {} {}.".format(wallet_transaction_code,
                                                                         member.currency, amount,
                                                                         circle.circle_name,
                                                                         member.currency, wallet_balance)
                     shares_desc = "{} confirmed. You have purchased shares " \
                                   "worth {} {}".format(shares_transaction_code, member.currency, amount)
                     circle_instance = circle_utils.Circle()
                     circle_member = CircleMember.objects.create(
                         circle=circle, member=member)
                     created_objects.append(circle_member)
                     wallet_transaction = Transactions.objects.create(
                         wallet=request.user.member.wallet,
                         transaction_type="DEBIT",
                         transaction_time=datetime.datetime.now(),
                         transaction_amount=amount,
                         transaction_desc=wallet_desc,
                         recipient=circle.circle_acc_number,
                         transaction_code=wallet_transaction_code,
                         source="wallet")
                     created_objects.append(wallet_transaction)
                     shares = Shares.objects.create(
                         circle_member=circle_member)
                     shares_transaction = IntraCircleShareTransaction.objects.create(
                         shares=shares,
                         transaction_type="DEPOSIT",
                         recipient=circle_member,
                         transaction_time=datetime.datetime.now(),
                         transaction_desc=shares_desc,
                         num_of_shares=amount,
                         transaction_code=shares_transaction_code)
                     wallet_serializer = WalletTransactionsSerializer(
                         wallet_transaction)
                     shares_serializer = SharesTransactionSerializer(
                         shares_transaction)
                     circle_member_serializer = UnloggedCircleMemberSerializer(
                         member, context={"circle": circle})
                     loan_limit = loan_instance.calculate_loan_limit(
                         circle, member)
                     fcm_instance = fcm_utils.Fcm()
                     old_circle_status = circle.is_active
                     is_active = old_circle_status
                     if not old_circle_status:
                         new_circle_status = circle_instance.check_update_circle_status(
                             circle)
                         if new_circle_status:
                             fcm_data = {
                                 "request_type": "UPDATE_CIRCLE_STATUS",
                                 "circle_acc_number":
                                 circle.circle_acc_number,
                                 "is_active": True
                             }
                             is_active = True
                             registration_ids = fcm_instance.get_circle_members_token(
                                 circle, None)
                             fcm_instance.data_push("mutiple",
                                                    registration_ids,
                                                    fcm_data)
                             print(fcm_data)
                     # unblock task
                     updating_loan_limit.delay(circle.id, member.id)
                     fcm_data = {
                         "request_type": "NEW_CIRCLE_MEMBERSHIP",
                         "circle_acc_number": circle.circle_acc_number,
                         "circle_member": circle_member_serializer.data
                     }
                     registration_ids = fcm_instance.get_circle_members_token(
                         circle, member)
                     fcm_instance.data_push("mutiple", registration_ids,
                                            fcm_data)
                     print(fcm_data)
                     ref = False
                     if existing_circle_member == 0:
                         try:
                             is_invited = CircleInvitation.objects.get(
                                 phone_number=member.phone_number,
                                 invited_by__circle=circle)
                             referral_fee = settings.REFERRAL_FEE
                             today = datetime.date.today()
                             if is_invited.time_invited.date(
                             ) == today and today.weekday() == 4:
                                 referral_fee = settings.FRIDAY_REFERRAL_FEE
                             ref = True
                             referral_programme_promotion.delay(
                                 is_invited.id, referral_fee)
                         except CircleInvitation.DoesNotExist:
                             print("object does not exist")
                         except CircleInvitation.MultipleObjectsReturned:
                             ReferralFee.objects.create(
                                 member=member,
                                 circle=circle,
                                 is_disbursed=False,
                                 extra_info=
                                 "user has been invited by more than one circle member"
                             )
                     if not ref:
                         CircleInvitation.objects.filter(
                             phone_number=member.phone_number,
                             invited_by__circle=circle).delete()
                     data = {
                         "status": 1,
                         "wallet_transaction": wallet_serializer.data,
                         "shares_transaction": shares_serializer.data,
                         "loan_limit": loan_limit,
                         "is_active": is_active
                     }
                 except Exception as e:
                     print(str(e))
                     instance = general_utils.General()
                     instance.delete_created_objects(created_objects)
                     data = {
                         "status": 0,
                         "message": "Unable to add member to circle"
                     }
                     return Response(data, status=status.HTTP_200_OK)
                 fcm_available_shares = circle_instance.get_guarantor_available_shares(
                     circle, member)
                 fcm_instance = fcm_utils.Fcm()
                 fcm_data = {
                     "request_type": "UPDATE_AVAILABLE_SHARES",
                     "circle_acc_number": circle.circle_acc_number,
                     "phone_number": member.phone_number,
                     "available_shares": fcm_available_shares
                 }
                 registration_ids = fcm_instance.get_circle_members_token(
                     circle, member)
                 fcm_instance.data_push("multiple", registration_ids,
                                        fcm_data)
                 return Response(data, status=status.HTTP_200_OK)
             data = {"status": 0, "message": response}
             return Response(data, status=status.HTTP_200_OK)
         data = {
             "status": 0,
             "message": "Unable to join circle.The circle is already full."
         }
         return Response(data, status=status.HTTP_200_OK)
     data = {"status": 0, "message": serializer.errors}
     return Response(data, status.HTTP_200_OK)
예제 #9
0
 def get_rating(self, guarantor):
     return loan_utils.Loan().calculate_circle_member_loan_rating(guarantor.loan.circle_member.member, guarantor.loan.circle_member.circle)