I'm looking for assistance with understanding this code. Several parts have commenting in regard to their function, however, towards the end there is very little to describe what the code section function is. Any assistance (or educated guess) with this is appreciated:

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

$db->startTransaction();

$inserted_records = 0;
foreach ($id_array as $id){
$video_id = (int)PT_Secure($id);

// get video data
$video = $db->where('id', $id)->getOne(T_VIDEOS);

// use the video play price if any, or the default price
$video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost;

// credit the uploader 50% of the video cost
$uploader_amount = $video_cost_new *0.50;

$time_start = microtime(true);

// add data to paid table
$insert_buy = $db->insert('paid_videos', [
'id_user' => $user_id,
'video_play_price' => (string)$video_cost_new,
'id_video' => $video_id,
'user_id_uploaded' => $video->user_id, // the user who uploaded the video
'video_title' => $video->title, // storing the title
'earned_amount' => $uploader_amount,
'time' => $time_start,
'short_id' => $video->short_id,
'session_key' => $_SESSION['session_key']
]);

// count successful inserted records
if ($insert_buy) {
$inserted_records++;
}

//update the 'balance' of the user who uploaded the video
// get the user's record
$userwallet = $db->where('id', $video->user_id)->getOne(T_USERS);

// add to the balance
$videouserwallet = $userwallet->balance+$uploader_amount;

// update the record
$db->where('id', $video->user_id);
$update_balance = $db->update(T_USERS, [
'balance' => number_format($videouserwallet, 1, '.', ''),
]);
}

$update_wallet = null;
$update_user_balance = null;

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

}else if ($wallet + $balance >= $amount) {
$balance = (string)($balance - ($amount - $wallet));
$wallet = (string)($wallet - $wallet);
$db->where('id', $user_id);
$update_user_balance = $db->update(T_USERS, [
'balance' => $balance
]);

$update_wallet = $db->update(T_USERS, [
'wallet' => $wallet
]);

} else {
echo json_encode([
'status' => 400,
'error_num' => 1,
'error' => 'Not enough money'
]);
exit();
}

    Debiting the account by the looks of it.

      Here are the comments you had in the thread where this logic was developed -

      if($wallet >= $amout) {
         // take money first from wallet
      } elseif ($wallet + $balance >= $amout) {
         // take money first from wallet and/or balance
      } else {
      

      Note; If you switch to INSERTing a row in an accounting table for each transaction that affects a value, all this logic will be simplified. You would just test if the SUM() of the amounts in a user's account is greater than or equal to $amount and insert a new row with a negative $amount into the table, along with the user's id and the id of the video purchase record (the last insert id from the INSERT query for the paid_videos table.)

      The reason I keep stating to do this, in addition to fixing the functional problems in the code, it simplifies all the code/queries.

        Instead of commenting, maybe move such operations into function definitions, and give the functions names that accurately (but hopefully fairly tersely) describe what they do and/or return. Well structured code with appropriate variable and function names seldom needs any comments. They can be reserved for weird/abnormal edge cases where you want to remind yourself a year from now why you coded it that way. 🙂

        Write a Reply...