fdesanto VIP
Total posts: 34
19 Oct 2015 10:59

Hi guys, I have plans like these: - Free - Lifetime - Basic - 1 year - Pro - 1 year

I need to set upgrade option from Basic to Pro. It's possible? if not, there is a workaround?

I see in "Cross Plan" tab "Upgrade" section, but seems to not work and there is a tip like "NOT YET WORKING".

Feedback is greatly appreciated Thanks

Last Modified: 23 Oct 2015

Tags Emerald 10


Sergey
Total posts: 13,748
20 Oct 2015 13:30

What kind of upgrade?

In crossplan you can set up to subtract price of already purchased subscription from current one. The parameters group is called Price affect.

Unfortunately there is no update option that would recalculate how many days was used and how much in particualr user have to pay for new plan.


fdesanto VIP
Total posts: 34
20 Oct 2015 14:11

Hi, with "upgrade" I mean from Basic to Pro, so Pro price needs to be reduced from the unused amount of the Basic plan.

Example: - User subscribes Basic Plan (during 1 year) - After 6 months the user wants to upgrade to Pro plan - I need to discount 6 month of unused amount of Basic to upgrade Pro Plan

Unfortunately there is no update option that would recalculate how many days was used and how much in particualr user have to pay for new plan.

Some workaround? can you point me on right way to develop some plugin to do this?


Sergey
Total posts: 13,748
20 Oct 2015 14:22

fdesanto Some workaround? can you point me on right way to develop some plugin to do this?

The only way is for me to finaly finish this option in cross plan. It is already there. Let me see what i can do.


fdesanto VIP
Total posts: 34
20 Oct 2015 14:43

Great Thanks.


pepperstreet VIP
Total posts: 3,837
20 Oct 2015 17:02

Sergey It is already there. Let me see what i can do.

+1 ;)


fdesanto VIP
Total posts: 34
21 Oct 2015 10:21

Hi, sorry for multiple posts, here a complete code to calculate unutilized amount of previous plans, this works with your parameters into "Cross Plan" section (also with parameter Price recalculation step in days).

You can copy this code in file /site/helpers/emerald.php at line 804 (function getPlanDetails).

NOTE 1 - Missing code to Deactive old plan after upgrade, now I manage this by using "Deactivate plans" in plan parameters. NOTE 2 - I have used "floor" instead "ceil" function (described on "Price recalculation step in days" tip), because with "ceil" your example not work as expected. Now with "floor" function you have expected result.

I hope with this you can release new version with this features in a shot time.

        // Upgrade if not others discounts
        if($plan->params->get('crossplans.ud_plans') && $plan->params->get('crossplans.ud_price_step') && $plan->params->get('crossplans.ud_price_step')>0 && $plan->discount == 0){ 

            $from_plans=$plan->params->get('crossplans.ud_plans');  // From Plans

            $user_plans = self::getUserPlans(NULL,true); // User active subscriptions

            foreach($from_plans as $from_plan){
                if(in_array($from_plan,$user_plans) && !in_array($plan->id,$user_plans)) 
                {
                    // Get active "from plan" subscription 
                    $user_subs = self::getUserActiveSubscriptions(false,0,$from_plan);

                    // Get Days,Days Enable and Price from DB for active Subscription
                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true);

                    $query->select('u.price');
                    $query->select('IF(u.ctime <= NOW(), (TO_DAYS(u.extime) - TO_DAYS(NOW())), (TO_DAYS(u.extime) - TO_DAYS(u.ctime)) ) as days');
                    $query->select('(TO_DAYS(u.extime) - TO_DAYS(u.ctime))  as days_enable');
                    $query->from('#__emerald_subscriptions AS u');

                    $query->where('u.id = '.array_values($user_subs)[0]->id);

                    $db->setQuery($query);

                    $result=$db->loadAssoc();

                    $plan->params->get('crossplans.ud_price_step');

                    // Cast to numeric for operations
                    $days_enable=intval($result['days_enable']);
                    $days=intval($result['days']);
                    $price=floatval($result['price']);

                    // Calculate Discount for Upgrade
                    if($days_enable>0 && $days>0) // To avoid problems
                    {
                        $total_steps=$days_enable/$plan->params->get('crossplans.ud_price_step');
                        $step_price=$price/$total_steps;
                        $passed_steps=floor($days/$plan->params->get('crossplans.ud_price_step'));
                        $discount_upgrade=$step_price*$passed_steps;
                    }
                    else
                    {
                        $discount_upgrade=0;
                    }

                    // Apply Discount
                    $plan->price -= $discount_upgrade;
                    $plan->discount += $discount_upgrade;
                }
            }
        }

fdesanto VIP
Total posts: 34
21 Oct 2015 13:23

Hi, still here a changes to deactive upgraded plans: in file /site/helpers/emerald.php at line 145 replace this:

$unpublish = $plan->params->get('crossplans.plans_deactivate');

with this:

$unpublish = array_unique(array_merge( (array) $plan->params->get('crossplans.plans_deactivate'), (array) $plan->params->get('crossplans.ud_plans')));

simply merge 2 options "crossplans.plans_deactivate" and "crossplans.ud_plans"


Sergey
Total posts: 13,748
21 Oct 2015 18:15

fdesanto simply merge 2 options "crossplans.plans_deactivate" and "crossplans.ud_plans"

Why?


fdesanto VIP
Total posts: 34
21 Oct 2015 18:29

hi, because when you upgrade a plan you get a discount of unitilized amount from previous plan, so old plan need to be deactivated.


fdesanto VIP
Total posts: 34
21 Oct 2015 18:39

unutilized amount is like partial refund of previous plan, otherwise user have 2 plans with 2 different expiration date, this not make a sense, because new plan include previous plan as upgrade


Sergey
Total posts: 13,748
22 Oct 2015 04:46

OK, I get it. After upgrade previose plan have to be unpublished.

Anyway I do not think your general code is correct. You code asume that $plan->params->get('crossplans.ud_plans') is a list of plans from which your can upgrade. But it is vice versa. It is list of the plans that user may upgrade to. So the logic have to be reversed.

  1. We hel list plan _Ds of active user subscritpions
  2. We get all those plans
  3. We cycle through and see to every plan parameter $plan->params->get('crossplans.ud_plans') and check if this plan in that list and if yes we remember it
  4. If there are more than one plan we have to find user subscription that can deduct most amount in favor of end user. So we calculate deduct amount for every user subscritpion of each pland that contain this plan in $plan->params->get('crossplans.ud_plans').

Something like. Anyway, do not worry, I'll try to manage that.


Sergey
Total posts: 13,748
23 Oct 2015 13:10

How? Are there no parameters in cross plan section of the plan?

Powered by Cobalt