Jump to content

recurring overwrite array


dadamssg87

Recommended Posts

I'm trying to automate and calculate a monthly subscription fee for my web service and i'm having a hell of a time getting it to work.

 

Basically, my users can upgrade their service anytime and i keep track of every upgrade/downgrade they make. I'm trying to calculate it based on days. It is a monthly fee but if they upgrade or downgrade, i'd like to prorate/credit their account.

 

First i'm pulling all of the accounts into an array. Then i'm pulling all of the activity(changes) the users have made. cycling through each account and then cycling through each account change. If the account change matches the current username calculate the fee per day for the entire month based on those changes and stick that in an array. Then if they've made another change in the same month, overwrite the specific days in the array to reflect that.

 

<?php
	$days_in_month = cal_days_in_month(CAL_GREGORIAN, date("n"), date("Y"));
	$year = date("Y");
	$month = date('n');

	$accounts  = $CI->account_model->get_all_accounts_to_bill();
	$activity  = $CI->account_model->get_all_activity();

	foreach($accounts as $username => $value)
	{			
		foreach($activity as $key => $data)
		{
			if($activity[$key]['username'] == $username)
			{
                                        //if first activity, set the start date to the first of the month
				if(empty($x_date)){$start_date = strtotime("$year-$month-01");}else{$start_date = strtotime($x_date);}
				$day    = (int) date("d",$start_date);
				$x_date = date("Y-m-d",strtotime($activity[$key]['created']));
                                        
                                        //calculate the monthly fee *i'm good here*
				if($activity[$key]['number_of_rooms'] == 0){$fee = 0.00;}
				elseif($activity[$key]['number_of_rooms'] == 1){$fee = $activity[$key]['base_fee'];}
				else{$fee = $activity[$key]['base_fee'] + (($activity[$key]['number_of_rooms'] - 1) * $activity[$key]['per_room_fee']);}

                                        //cycle through the relevant days and calculate a per day fee..Write in array
                                        //This is where i'm having trouble. I don't know if im using the $day variable right					
				for($i = $day; $i <= $days_in_month; $i += 1)
				{
					$user[$username][$i] = $fee/$days_in_month;
				}

				unset($i);
				unset($day);
			}
		}

		unset($x_date);
		$sum = 0;
		if(isset($user[$username]) && count($user[$username]) > 0)
		{
			foreach($user[$username] as $day => $fee)
			{
				$sum = $sum + $fee;
			}
			$user[$username]['total'] = $sum;
		}
	}

?>

 

I'm pulling the activity rows from the database by 'created' ascending.

 

database rows for one user below..(id, username, created, base_fee, per_room_fee, credit_balance, number_of_rooms)

		2	38	2011-08-01 09:45:00	27.99	7.99   0.00	0
		6	38	2011-08-09 10:00:01	27.99	7.99	  0.00	2		
		7	38	2011-08-30 10:25:15	27.99	7.99	  0.00	5	

 

the code right now produces the array below when it should be the first 8 days should be 0.00, the 21 days should be 1.16, and the last 2 days should be 1.93

[38] => Array
        (
            [1] => 1.16064516129
            [2] => 1.16064516129
            [3] => 1.16064516129
            [4] => 1.16064516129
            [5] => 1.16064516129
            [6] => 1.16064516129
            [7] => 1.16064516129
            [8] => 1.16064516129
            [9] => 1.93387096774
            [10] => 1.93387096774
            [11] => 1.93387096774
            [12] => 1.93387096774
            [13] => 1.93387096774
            [14] => 1.93387096774
            [15] => 1.93387096774
            [16] => 1.93387096774
            [17] => 1.93387096774
            [18] => 1.93387096774
            [19] => 1.93387096774
            [20] => 1.93387096774
            [21] => 1.93387096774
            [22] => 1.93387096774
            [23] => 1.93387096774
            [24] => 1.93387096774
            [25] => 1.93387096774
            [26] => 1.93387096774
            [27] => 1.93387096774
            [28] => 1.93387096774
            [29] => 1.93387096774
            [30] => 1.93387096774
            [31] => 1.93387096774
            [total] => 53.7641935484
        )

Link to comment
Share on other sites

got it

 

<?php
	$accounts  = $CI->account_model->get_all_accounts_to_bill();
	$activity  = $CI->account_model->get_all_activity();

	$array     = $accounts + $activity;


	foreach($accounts as $username => $value)
	{			
		foreach($activity as $key => $data)
		{
			if($activity[$key]['username'] == $username)
			{
				//if first activity
				if(empty($counter))
				{
					$day = 1;
				}
				else
				{
					$day = (int) date("d",strtotime($activity[$key]['created']));						
				}

				//set 
				$counter = "first activity down"; // <-- tells program that it's been through an activity before

				//calculate monthly fee
				if($activity[$key]['number_of_rooms'] == 0){$fee = 0.00;}
				elseif($activity[$key]['number_of_rooms'] == 1){$fee = $activity[$key]['base_fee'];}
				else{$fee = $activity[$key]['base_fee'] + (($activity[$key]['number_of_rooms'] - 1) * $activity[$key]['per_room_fee']);}

				//write relevant day/fee to array					
				for($i = $day; $i <= $days_in_month; $i += 1)
				{
					$users[$username]['days'][$i] = $fee/$days_in_month;
				}
			}
		}

		//unsets counter for next account
		unset($counter);

		//makes sure account was calculated 
		$sum = 0;
		if(isset($users[$username]) && count($users[$username]['days']) > 0)
		{
			foreach($users[$username]['days'] as $day => $fee)
			{
				$sum = $sum + $fee;
			} 
			unset($users[$username]['days']); // delete to get days - fee in the return array
			$users[$username]['total']          = money_format('%i',$sum);
			$users[$username]['cim_profile_id'] = $accounts[$username]['cim_profile_id'];
		} 
	}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.