Cron Job file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Cron Job file

Post by nite4000 »

hey everyone i have a task I am trying to accomplish. least this is 2 of 2 the other one i havent completed yet.

but I have an option that will allow the investment or principal to be return when the investment is completed. but I am not sure where in the cron job to put it.


Here is the cron job code

Code: Select all


function get_active_accounts()
{
	$query  = "SELECT * FROM accounts WHERE status = 'Active'";
	$result = mysql_query($query);

	$accounts = array();
	while ($row = mysql_fetch_assoc($result))
	{
		$accounts[] = $row;
	}
	
	return $accounts;
}

function get_account_plans($account)
{
	
	
	$query = "SELECT inv.id, inv.username,inv.user_id,inv.plan_id,inv.plan_name, inv.total_invest, inv.rate, inv.`date`,
					 plans.parent, packages.days
			  FROM investments AS inv
			  INNER JOIN plans ON inv.plan_id = plans.id
			  INNER JOIN packages ON plans.parent = packages.id
			  WHERE inv.user_id = {$account}
			    AND inv.end_date > NOW()";
	$result = mysql_query($query);

	$plans = array();
	while ($row = mysql_fetch_assoc($result))
	{
		$plans[] = $row;
	}
	
	return $plans;
}

function need_to_pay($last_paid, $pay_frequency)
{
	$last_payment_date = strtotime($last_paid);
	$days_since_last_payment = floor((time() - $last_payment_date) / 86400);
	if ($days_since_last_payment >= $pay_frequency)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}


function credit_account($account, $investment, $rate)
{
	global $debug;
	$earnings = $investment * $rate / 100;
	$query = "UPDATE accounts
			  SET acct_balance = acct_balance + {$earnings},
			      earned_total = earned_total + {$earnings}
			  WHERE id = {$account}";
			  
			if($today == $entry['end_date']){

		  
	if ($debug === TRUE)
	{
		echo nl2br($query) . "<br /><br />";
	}
	else
	{
		mysql_query($query) or die(mysql_error());
	}
}
}





function update_pay_date($investment)
{
	global $debug;
	$query = "UPDATE investments
			  SET `date` = NOW()
			  WHERE id = {$investment}";
	if ($debug === TRUE)
	{
		echo nl2br($query) . "<br /><hr />";
	}
	else
	{
		mysql_query($query) or die(mysql_error());
	}
}

$to_pay = array();
$accounts = get_active_accounts();
if ($debug === TRUE) { echo "Active accounts:<br />"; }
foreach ($accounts as $account)
{
	if ($debug === TRUE) { echo $account['id'] . "<br />"; }
	$plan = get_account_plans($account['id']);
	if (!empty($plan))
	{
		$to_pay[] = $plan;
	}
}

if ($debug === TRUE)
{
	echo "<hr />";
	echo "Accounts to be paid:<br /><br />";
}

foreach ($to_pay as $payee)
{
	foreach ($payee as $entry)
	{
		if (need_to_pay($entry['date'], $entry['days']) === TRUE)
		{
			if ($debug === TRUE)
			{
				echo "Account: {$entry['user_id']}<br /><br />";
			}
			credit_account($entry['user_id'], $entry['total_invest'], $entry['rate']);
			update_pay_date($entry['id']);
		
	


				$today = date("Y-m-d", time());
				$earn = $entry['total_invest'] * $entry['rate'] / 100;
$query=mysql_query("INSERT into earnings(id,user_id,amount,plan,date)VALUES(null,'{$entry['user_id']}','{$earn}','{$entry['plan_name']}',NOW())");
}
			
}							
	}	





I need to add back the total_invest field which holds the amount invested back the accounts but not sure what to edit or add. as I said this will work with a chk box option called return_principal

i want to do it like

if checkbox=='1' then return principal else dont.


anyone who can help would be great.
Post Reply