I am using a php web video script which allows Users to purchase videos successfully. The purchases are made from the amount available in the Users’ 'wallet' (the User can also earn compensation, which gets added to his earnings 'balance').

When there’s not enough available in the 'wallet' for the purchase, the script checks and displays a message 'not enough money'.

I’ve added the ability where the script first checks the 'wallet' amount, and if empty will then check the earnings 'balance', and use amount from the earnings 'balance', for the purchase, and if neither has enough, then the “not enough money” message appears.

However, if the minimum cost of a purchase is '2' and the 'wallet' has '1' left over, it will never get used.
So, I'm trying to code it so, after checking , the 'wallet', and finds it doesn't have enough, it will combine the 'wallet' with the earnings 'balance', to check if there is enough. If enough > purchase. If not enough show "not enough money". But this code keeps showing 'not enough money' message, even though the 'wallet' has '4', and the earnings 'balance' has '4', and the purchase amount is '6'. I was hoping the code would combine 4 + 4 = 8 and then deduct the 6 (purchase amount).

Can you please look at my code and tell me what might be incorrect? Or suggest something that will work?

    if($wallet >= $amount){

                    $wallet = (string)($wallet - $amount);
                    $db->where('id', $user_id);
                    $update_wallet = $db->update(T_USERS, [
                        'wallet' => $wallet
                    ]);

        }else{

        if($balance + $wallet >= $amount){

        $wallet = (string)($amount - $wallet);
        //$balance = (string)($balance + $wallet);
        $balance = (string)($balance + $wallet - $amount);
        //$balance = (string)($balance - $amount);
        //$wallet = '0';

        $db->where('id', $user_id);
        $update_user_balance = $db->update(T_USERS, [
        'balance' => $balance
        ]);
        }
        }

Any help/comment/suggestion is appreciated

What's the point of the balance AND the wallet? Why not pass any compo straight to the wallet? Could you do away with the balance?
The way I see it, you just need a transactions table with something like ... [ user_id | amount | transaction_type | trans_datetime ... ]
So positive amounts for compensation and money re-loads, negative amounts for purchases and cash-outs. Perhaps your system is more sophisticated than that?

If not, you just need to do a SUM() on your amounts in the db table to get the existing balance.
If you have a ridiculous number of records, then a non-normalized table with running balance may be easier, but still without the need to combine moneys from different sources (balance + wallet).

If you are stuck with the method you have...

$total = $balance + $wallet;
if($amount > $total){
    echo "Not enough funds";
    //no update
}elseif( $amount <= $wallet){
    echo "Funds in wallet will cover this! :)";
    //take money out of wallet only
}else{
    $fromBalance = $amount - $wallet;
    echo "$wallet from wallet and $fromBalance from balance will cover this";
    //empty wallet and take money from balance
    //OR you can give user choice of using money from balance or declining the purchase.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.