I've been working on importing our existing set of website subscriptions into a Commerce License / License Billing backed framework. I talked this out in #drupal-commerce yesterday:

(9:24:31 AM) Kazanir: hey bojanz, is there any existing path to migrating an existing set of subscriptions into a commerce license + recurring setup
(9:24:57 AM) bojanz: Kazanir: no
(9:24:58 AM) Kazanir: i.e. is there a set of functions somewhere I can use to duplicate the Rules functions that create the recurring order for a new license, or am I basically in the Wild West here
(9:31:44 AM) Kazanir: it looks to me like I should be able to use some of the logic in commerce_license_billing_create_recurring_orders -- generate a commerce license entity from the migrating data, put that as the single element of a licenses array, open a billing cycle to match, and then use commerce_license_billing_generate_recurring_order($order, $billing_cycle, $licenses);
(9:32:04 AM) Kazanir: If I am missing a key step anywhere in there, advice would be extremely welcome :)

I proceeded to do this and it more or less worked. I created the license entity using logic similar to the line_item_presave hook, faked a "previous order" entity object, created a matching billing cycle using the logic from the Rules action, and passed those to:

commerce_license_billing_generate_recurring_order($order, $billing_cycle, $licenses);

The problem I had was that the line item for the recurring order would pro-rate itself because of a mismatch between days in months. Since I have a specified end date for the billing cycle (because we are importing existing data), I set the license start date to be one month prior so that it would "match" the next recurring order. I did this with the reverse of the logic from the normal monthly billing cycle:

<?php
  $billing_cycle->end = $expire_ts; // Known expiration date
  $billing_cycle->start = strtotime("-1 month", $expire_ts);
?>

But I'm still hitting a proration issue, as follows:

1. My migrated license has an expiration date of (for example) April 15th. Setting the start date to -1 month gives us March 15th. That means the intervening time is 31 days.

2. The recurring order created has a line item which also specifies a month differential. This becomes April 15th to May 15th. That's 30 days.

This results in commerce_license_billing_prorate_charges() comparing those two against each other and pro-rating the April-to-May charge down to 30/31 (96.8%) of the correct monthly charge.

So I guess I have two questions:

a. Am I doing something wrong, or not understanding the data structure that I should be creating to migrate my existing subscriptions?

b. If not, is this an issue that is experienced during normal usage of asynch monthly license subscriptions?

CommentFileSizeAuthor
#3 2207421-dont-prorate-prepaid-2.patch1.73 KBbojanz

Comments

Kazanir’s picture

Issue summary: View changes
bojanz’s picture

I'm afraid I don't understand the month acrobatics you are trying to do.
Proration is always done for the licenses against the current billing cycle, so it shouldn't matter how many days the next or previous month has.

EDIT: Kazanir explained on IRC.

bojanz’s picture

Title: Monthly billing cycle variation and proration » Don't prorate prepaid licenses for the next cycle
Category: Support request » Bug report
Status: Active » Needs review
StatusFileSize
new1.73 KB

A prepaid license is always paid for the next cycle. Since it always covers the whole cycle, there is no need to prorate it. That avoids the problem completely.

Can you please try this patch?

bojanz’s picture

Status: Needs review » Fixed

Committed the patch. Reopen if you encounter any related issues.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.