From ed84213fd24efdea44e9a688b9f501ec2f30e169 Mon Sep 17 00:00:00 2001 From: Charles-Henri Decultot Date: Wed, 5 Nov 2025 12:03:02 +0100 Subject: [PATCH 1/2] fix: Booking credit allocation issues --- .../doctype/booking_credit/booking_credit.py | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/erpnext/venue/doctype/booking_credit/booking_credit.py b/erpnext/venue/doctype/booking_credit/booking_credit.py index a628e5c64e..b0f59f2a8c 100644 --- a/erpnext/venue/doctype/booking_credit/booking_credit.py +++ b/erpnext/venue/doctype/booking_credit/booking_credit.py @@ -4,7 +4,7 @@ from collections import defaultdict import frappe -from frappe.utils import add_to_date, cint, flt, get_datetime, getdate, now_datetime, nowdate +from frappe.utils import add_to_date, cint, flt, get_datetime, getdate, now_datetime, nowdate, add_days from erpnext.controllers.status_updater import StatusUpdater from erpnext.venue.doctype.booking_credit_ledger.booking_credit_ledger import create_ledger_entry @@ -148,11 +148,16 @@ def _add_booking_credits(doc): def automatic_booking_credit_allocation(subscription): for rule in subscription.booking_credits_allocation: - if last_generated_credit := get_last_credit_for_customer( + last_generated_credit = get_last_credit_for_customer( subscription.customer, rule.booking_credit_type, subscription=subscription.name - ): - if not are_subscription_credits_due(last_generated_credit[0].date, rule, subscription): - continue + ) + + if last_generated_credit and last_generated_credit[0].date == getdate(nowdate()): + return + + previous_date = last_generated_credit[0].date if last_generated_credit else None + if not are_subscription_credits_due(rule, subscription, previous_date=previous_date): + continue booking_credit = frappe.get_doc( { @@ -161,7 +166,7 @@ def automatic_booking_credit_allocation(subscription): "customer": subscription.customer, "quantity": cint(rule.quantity), "subscription": subscription.name, - "expiration": rule.expiration, + "expiration_date": add_days(nowdate(), rule.expiration), "booking_credit_type": rule.booking_credit_type, } ) @@ -170,14 +175,17 @@ def automatic_booking_credit_allocation(subscription): booking_credit.submit() -def are_subscription_credits_due(date, rule, subscription): +def are_subscription_credits_due(rule, subscription, previous_date=None): from dateutil import rrule + if not previous_date: + return True + if rule.recurrence == "Once": return False if rule.recurrence == "Every Billing Period": - if getdate(date) != getdate() and getdate() == getdate(subscription.current_invoice_start): + if getdate(previous_date) != getdate() and getdate() == getdate(subscription.current_invoice_start): return True return False @@ -205,12 +213,14 @@ def are_subscription_credits_due(date, rule, subscription): ) ] - if getdate(date) != getdate() and getdate() in possible_dates: + if getdate() in possible_dates: return True + return False + def get_last_credit_for_customer(customer, booking_credit_type, subscription): - filters = {"customer": customer, "booking_credit_type": booking_credit_type, "docstatus": 1} + filters = {"customer": customer, "booking_credit_type": booking_credit_type, "docstatus": 1, "subscription": subscription} return frappe.get_all( "Booking Credit", filters=filters, fields=["name", "date"], order_by="date DESC", limit=1 -- GitLab From 45eb924ad514b7c445c6390e75bd164d79d61a3a Mon Sep 17 00:00:00 2001 From: Charles-Henri Decultot Date: Wed, 5 Nov 2025 12:04:38 +0100 Subject: [PATCH 2/2] fix: check that current day is different from previous date --- erpnext/venue/doctype/booking_credit/booking_credit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/venue/doctype/booking_credit/booking_credit.py b/erpnext/venue/doctype/booking_credit/booking_credit.py index b0f59f2a8c..44439277d0 100644 --- a/erpnext/venue/doctype/booking_credit/booking_credit.py +++ b/erpnext/venue/doctype/booking_credit/booking_credit.py @@ -213,7 +213,7 @@ def are_subscription_credits_due(rule, subscription, previous_date=None): ) ] - if getdate() in possible_dates: + if getdate(previous_date) != getdate() and getdate() in possible_dates: return True return False -- GitLab