Author Topic: Add Bank Interest for a user.  (Read 5520 times)

Offline JRSofty

Add Bank Interest for a user.
« on: August 01, 2006, 06:16:08 pm »
Ok this one is really tricky. I have no way of packaging it however I'm willing to let anyone who want's to take a shot of including it into the Shop system to do so.

Requirements are of course that you use SMFShop (I originally designed it under 2.0 however it still works under 2.2) You will need to have a way to change the database as you will need to create a new column in the members table. You will also need to change the dointerest.php file a little bit.

Step 1. Create a new column in the smf_members (or what ever prefix you are using) table called 'bankInterest'. It should be an integer and the default should be 0 as this would not allow modification of the standard bank interest that is part of the SMFShop system.

Step 2. Place this script into the Sources/shop/items directory:
Code: [Select]
<?php
/**********************************************\
| SMFSHOP (Shop MOD for Simple Machines Forum) |
|         (c) 2005 DanSoft Australia           |
|      http://www.dansoftaustralia.com/        |
\**********************************************/

//File: AddToInterest.php
//      Item (takes additional fields when adding to Admin panel)

//VERSION: 1.0 
//DATE: 13th February 2006
//AUTHOR: JRSofty Programming

class item_AddToInterest extends itemTemplate {
    function 
getItemDetails() {
        
$this->name "Add xxx to Interest Rate";
        
$this->desc "Increase your Interest Rate by xxx";
        
$this->price 100;
        
        
$this->require_input false;
        
$this->can_use_item true;
    }
    
    
//this is the best bit of this item. When you add the item into the admin panel, you
    //can set additional variables (up to 4). Make sure to call them info1, info2, info3
    //and info4.
    
function getAddInput() {
        return 
"Amount to change Interest Rate by: <input type='text' name='info1' value='1'>";
    }

    
//the additional parameters (see 'getAddInput' above) are in the '$item_info array.
    //make sure to make it global (like shown here) otherwise you won't be able to access
    //it's contents. THE ARRAY IS 1-BASED (eg. 1 IS THE FIRST ITEM) NOT 0-BASED!
    
function onUse() {
        global 
$db_prefix$ID_MEMBER$item_info;

        
$result db_query("UPDATE {$db_prefix}members
                            SET bankInterest = bankInterest + 
{$item_info[1]}
                            WHERE ID_MEMBER = 
{$ID_MEMBER}",
                            
__FILE____LINE__);
        return 
"Successfully added {$item_info[1]} to interest!";
        
        
//echo $item_info[1];
        
die("dead");
    }

}

?>

Step 3: Here is the dointerest.php that I use so that the interest increase is picked up from the member's row in the table and added to the bank interest.
Code: [Select]
<?php



include ('../../SSI.php');

$request db_query("SELECT `ID_MEMBER`, `bankInterest` 
FROM 
{$db_prefix}members",__FILE__,__LINE__);

while((
$row mysql_fetch_array($request)) !== false){

$interest_rate = ($modSettings['shopInterest'] + $row['bankInterest']) / 100;
db_query("UPDATE {$db_prefix}members
SET moneyBank = moneyBank + (moneyBank*
{$interest_rate}) WHERE `ID_MEMBER` = ".$row['ID_MEMBER'], __FILE____LINE__);

echo 
$row['ID_MEMBER'] . " Interest added at " $interest_rate " ".date("d/m/Y h:i:s A")."<br>";

}


?>

Known issues:
When you enter the Shop your updated or changed Interest rate does not display. It only displays what the SMFShop System Interest is set. Other than that it seems to work ok.

If anyone would like to package this please feel free (or if Daniel15 would like to include it in his next version that is also fine :D) the only thing I ask is that my name remain in place in the code.

Offline Daniel15

Re: Add Bank Interest for a user.
« Reply #1 on: August 04, 2006, 10:17:39 pm »
Quote
die("dead");
You could probably remove that from the code, I only used it for debugging :P

Anyways, if you want to learn how to package stuff for SMF, then take a look at the Package SDK. It has everything you need to know :) I don't have time to package it at the moment, though...

Offline JRSofty

Re: Add Bank Interest for a user.
« Reply #2 on: August 07, 2006, 04:11:57 pm »
Yeah Time is my problem as well ;)

I'll see if I can get it packaged.

Quick question about the dointerest.php script. In your script you have this statement if(!isset($_SERVER['HTTP_HOST'])). Is this required for a CRONJOB to operate correctly?

I'm just wondering because it seems none of the CRONJOB places that I've seen on the net seem to work for my dointerest.php call.
« Last Edit: August 07, 2006, 05:53:28 pm by JRSofty »

Offline Daniel15

Re: Add Bank Interest for a user.
« Reply #3 on: August 19, 2006, 12:42:30 pm »
Quote
Quick question about the dointerest.php script. In your script you have this statement if(!isset($_SERVER['HTTP_HOST'])). Is this required for a CRONJOB to operate correctly?
Oh, that code is for if someone tries to call the script via a web browser, through the URL of the page. The script will work fine if you're using a cron job on your web server

Quote
I'm just wondering because it seems none of the CRONJOB places that I've seen on the net seem to work for my dointerest.php call.
Well, they won't be able to, as they're calling the script via the URL (as opposed to running it on the server).
You'll want to use the slightly modified version available at http://www.daniel15.com/forum/index.php?topic=31.0 :)