Skip to content

Commit

Permalink
fix: prorate factor for subscription plan (#36953)
Browse files Browse the repository at this point in the history
(cherry picked from commit fc79af5)
(cherry picked from commit 9a15ed8)
  • Loading branch information
GursheenK authored and mergify[bot] committed Sep 10, 2023
1 parent 8b2c6ed commit d7f09f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
20 changes: 20 additions & 0 deletions erpnext/accounts/doctype/subscription/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,23 @@ def test_multicurrency_subscription(self):
# Check the currency of the created invoice
currency = frappe.db.get_value("Sales Invoice", subscription.invoices[0].invoice, "currency")
self.assertEqual(currency, "USD")

def test_plan_rate_for_midmonth_start_date(self):
subscription = frappe.new_doc("Subscription")
subscription.party_type = "Supplier"
subscription.party = "_Test Supplier"
subscription.generate_invoice_at_period_start = 1
subscription.follow_calendar_months = 1
subscription.generate_new_invoices_past_due_date = 1
subscription.start_date = "2023-04-08"
subscription.end_date = "2024-02-27"
subscription.append("plans", {"plan": "_Test Plan Name 4", "qty": 1})
subscription.save()

subscription.process()

self.assertEqual(len(subscription.invoices), 1)
pi = frappe.get_doc("Purchase Invoice", subscription.invoices[0].invoice)
self.assertEqual(pi.total, 55333.33)

subscription.delete()
23 changes: 11 additions & 12 deletions erpnext/accounts/doctype/subscription_plan/subscription_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,17 @@ def get_plan_rate(
prorate = frappe.db.get_single_value("Subscription Settings", "prorate")

if prorate:
prorate_factor = flt(
date_diff(start_date, get_first_day(start_date))
/ date_diff(get_last_day(start_date), get_first_day(start_date)),
1,
)
cost -= plan.cost * get_prorate_factor(start_date, end_date)
return cost

prorate_factor += flt(
date_diff(get_last_day(end_date), end_date)
/ date_diff(get_last_day(end_date), get_first_day(end_date)),
1,
)

cost -= plan.cost * prorate_factor
def get_prorate_factor(start_date, end_date):
total_days_to_skip = date_diff(start_date, get_first_day(start_date))
total_days_in_month = int(get_last_day(start_date).strftime("%d"))
prorate_factor = flt(total_days_to_skip / total_days_in_month)

return cost
total_days_to_skip = date_diff(get_last_day(end_date), end_date)
total_days_in_month = int(get_last_day(end_date).strftime("%d"))
prorate_factor += flt(total_days_to_skip / total_days_in_month)

return prorate_factor

0 comments on commit d7f09f8

Please sign in to comment.