def update_paid_amount(self): precision = cint(frappe.db.get_default("currency_precision")) or 2 loan = frappe.get_doc("Loan", self.against_loan) for payment in self.repayment_details: frappe.db.sql( """ UPDATE `tabLoan Interest Accrual` SET paid_principal_amount = `paid_principal_amount` + %s, paid_interest_amount = `paid_interest_amount` + %s WHERE name = %s""", (flt(payment.paid_principal_amount, precision), flt(payment.paid_interest_amount, precision), payment.loan_interest_accrual)) if flt(loan.total_principal_paid + self.principal_amount_paid, precision) >= flt(loan.total_payment, precision): if loan.is_secured_loan: frappe.db.set_value("Loan", self.against_loan, "status", "Loan Closure Requested") else: frappe.db.set_value("Loan", self.against_loan, "status", "Closed") frappe.db.sql( """ UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s WHERE name = %s """, (loan.total_amount_paid + self.amount_paid, loan.total_principal_paid + self.principal_amount_paid, self.against_loan)) update_shortfall_status(self.against_loan, self.principal_amount_paid)
def mark_as_paid(self): paid_entries = [] paid_amount = self.amount_paid interest_paid = paid_amount if not paid_amount: frappe.throw(_("Amount paid cannot be zero")) if self.amount_paid < self.penalty_amount: msg = _("Paid amount cannot be less than {0}").format(self.penalty_amount) frappe.throw(msg) if self.payment_type == "Loan Closure" and flt(self.amount_paid, 2) < flt(self.payable_amount, 2): msg = _("Amount of {0} is required for Loan closure").format(self.payable_amount) frappe.throw(msg) loan = frappe.get_doc("Loan", self.against_loan) if self.paid_accrual_entries: paid_accrual_entries = json.loads(self.paid_accrual_entries) if paid_amount - self.penalty_amount > 0 and self.paid_accrual_entries: interest_paid = paid_amount - self.penalty_amount for lia, interest_amount in iteritems(paid_accrual_entries): if interest_amount <= interest_paid: paid_entries.append(lia) interest_paid -= interest_amount elif interest_paid: self.partial_paid_entry = frappe.as_json({"name": lia, "interest_amount": interest_amount}) frappe.db.set_value("Loan Interest Accrual", lia, "interest_amount", interest_amount - interest_paid) interest_paid = 0 if paid_entries: self.paid_accrual_entries = frappe.as_json(paid_entries) else: self.paid_accrual_entries = "" if interest_paid: self.principal_amount_paid = interest_paid if paid_entries: frappe.db.sql("""UPDATE `tabLoan Interest Accrual` SET is_paid = 1 where name in (%s)""" #nosec % ", ".join(['%s']*len(paid_entries)), tuple(paid_entries)) if flt(loan.total_principal_paid + self.principal_amount_paid, 2) >= flt(loan.total_payment, 2): frappe.db.set_value("Loan", self.against_loan, "status", "Loan Closure Requested") frappe.db.sql(""" UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s WHERE name = %s """, (loan.total_amount_paid + self.amount_paid, loan.total_principal_paid + self.principal_amount_paid, self.against_loan)) update_shortfall_status(self.against_loan, self.principal_amount_paid)
def update_paid_amount(self): loan = frappe.get_value( "Loan", self.against_loan, [ "total_amount_paid", "total_principal_paid", "status", "is_secured_loan", "total_payment", "loan_amount", "disbursed_amount", "total_interest_payable", "written_off_amount", ], as_dict=1, ) loan.update({ "total_amount_paid": loan.total_amount_paid + self.amount_paid, "total_principal_paid": loan.total_principal_paid + self.principal_amount_paid, }) pending_principal_amount = get_pending_principal_amount(loan) if not loan.is_secured_loan and pending_principal_amount <= 0: loan.update({"status": "Loan Closure Requested"}) for payment in self.repayment_details: frappe.db.sql( """ UPDATE `tabLoan Interest Accrual` SET paid_principal_amount = `paid_principal_amount` + %s, paid_interest_amount = `paid_interest_amount` + %s WHERE name = %s""", ( flt(payment.paid_principal_amount), flt(payment.paid_interest_amount), payment.loan_interest_accrual, ), ) frappe.db.sql( """ UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s, status = %s WHERE name = %s """, (loan.total_amount_paid, loan.total_principal_paid, loan.status, self.against_loan), ) update_shortfall_status(self.against_loan, self.principal_amount_paid)
def update_paid_amount(self): loan = frappe.get_doc("Loan", self.against_loan) for payment in self.repayment_details: frappe.db.sql(""" UPDATE `tabLoan Interest Accrual` SET paid_principal_amount = `paid_principal_amount` + %s, paid_interest_amount = `paid_interest_amount` + %s WHERE name = %s""", (flt(payment.paid_principal_amount), flt(payment.paid_interest_amount), payment.loan_interest_accrual)) frappe.db.sql(""" UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s WHERE name = %s """, (loan.total_amount_paid + self.amount_paid, loan.total_principal_paid + self.principal_amount_paid, self.against_loan)) update_shortfall_status(self.against_loan, self.principal_amount_paid)
def on_submit(self): if self.loan: self.db_set("status", "Pledged") self.db_set("pledge_time", now_datetime()) update_shortfall_status(self.loan, self.total_security_value) update_loan(self.loan, self.maximum_loan_value)