diff --git a/app/integrations/simbrella.py b/app/integrations/simbrella.py index ccb7620..57cf740 100644 --- a/app/integrations/simbrella.py +++ b/app/integrations/simbrella.py @@ -92,9 +92,8 @@ class SimbrellaClient: vat_fee = loan_charges.get("VAT")['amount'] interest_fee = loan_charges.get("INTEREST")['amount'] insurance_fee = loan_charges.get("INSURANCE")['amount'] - + product_id = str(loan_data.get('productId', "")) debtId = str(loan_data.get('debtId', "")).strip().zfill(6) - product_id = str(loan_data.get('productId', '')) disbursement_data = { "transactionId": loan_data.get('transactionId'), "fbnTransactionId": loan_data.get('transactionId'), @@ -103,10 +102,10 @@ class SimbrellaClient: "accountId": loan_data.get('accountId'), "productId": str(loan_data.get('productId', "")), "provideAmount": loan_data.get('currentLoanAmount'), - "collectAmountInterest": interest_fee if product_id != '3MPC' else 0, #send charges for only 1 month loan - "collectAmountMgtFee": mgt_fee if product_id != '3MPC' else 0, - "collectAmountInsurance": insurance_fee if product_id != '3MPC' else 0, - "collectAmountVAT": vat_fee if product_id != '3MPC' else 0, + "collectAmountInterest": interest_fee if product_id != '3MPC' else 0, + "collectAmountMgtFee": mgt_fee, + "collectAmountInsurance": insurance_fee, + "collectAmountVAT": vat_fee, "countryId": "01", "comment": "Loan Disbursement", } @@ -148,7 +147,17 @@ class SimbrellaClient: result.get('responseMessage', '')) reload_loan = LoanService.get_loan_by_transaction_id(transaction_id=data['transactionId']) reload_loan_data = reload_loan.to_dict() + #mark repayment schedule as active + repayment_schedule = LoanRepaymentScheduleService.get_repayment_schedule_by_loan_id( + reload_loan_data['debtId'], include_paid=False) + logger.info(f'Loan repayment schedule: {repayment_schedule}') + if repayment_schedule: + for schedule in repayment_schedule: + logger.info(f"Updating repayment schedule ID {schedule.id} status to ACTIVE") + LoanRepaymentScheduleService.update_repayment_schedule_status_to_active(schedule.id) + SimbrellaClient.verify_disbursement_transaction(reload_loan_data) + return ResponseHelper.success(response.json(), "Successful") else: diff --git a/app/models/loan.py b/app/models/loan.py index c9dd5aa..fc29e1e 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -236,9 +236,15 @@ class Loan(db.Model): """ Get the latest loan without a disbursement date. """ - return cls.query.filter( - cls.disburse_date.is_(None) - ).order_by(cls.created_at.desc()).first() + logger.info("Fetching latest loan without disburse date") + + try: + return cls.query.filter( + cls.disburse_date.is_(None) + ).order_by(cls.created_at.desc()).first() + except Exception as e: + logger.error(f"Error fetching latest loan without disburse date: {e}") + raise @classmethod def get_latest_loan_with_disburse_date(cls): diff --git a/app/models/loan_repayment_schedule.py b/app/models/loan_repayment_schedule.py index a38d670..5592c8a 100644 --- a/app/models/loan_repayment_schedule.py +++ b/app/models/loan_repayment_schedule.py @@ -215,7 +215,25 @@ class LoanRepaymentSchedule(db.Model): logger.error(f"Error updating repayment schedule {schedule_id} after loan repayment: {e}") raise - + @classmethod + def update_repayment_schedule_status_to_active(cls, schedule_id): + """ + Update repayment schedule status to ACTIVE. + """ + try: + schedule = cls.query.get(schedule_id) + if not schedule: + raise ValueError(f"Schedule with ID {schedule_id} does not exist.") + schedule.paid_status = RepaymentScheduleStatus.ACTIVE + schedule.updated_at = datetime.now(timezone.utc) + db.session.commit() + logger.info(f"Updated repayment schedule ID {schedule_id} status to ACTIVE") + return schedule.to_dict() + except Exception as e: + db.session.rollback() + logger.error(f"Error updating repayment schedule status for schedule {schedule_id}: {e}") + raise + @classmethod def update_repayment_schedule_balance(cls, schedule_id, amount_collected): """ diff --git a/app/services/loan_repayment_schedule.py b/app/services/loan_repayment_schedule.py index 7aca7df..5c44c0e 100644 --- a/app/services/loan_repayment_schedule.py +++ b/app/services/loan_repayment_schedule.py @@ -32,7 +32,12 @@ class LoanRepaymentScheduleService: Update repayment schedule status. """ return LoanRepaymentSchedule.update_repayment_schedule_status(schedule_id) - + @classmethod + def update_repayment_schedule_status_to_active(cls, schedule_id): + """ + Update repayment schedule status. + """ + return LoanRepaymentSchedule.update_repayment_schedule_status_to_active(schedule_id) @classmethod def update_repayment_schedule_balance(cls, schedule_id, amount_collected): """